c# 我模拟http put提交 通过socket 直接发送请求 但是不对 为什么 每个地方都对的啊 求指点
private void button1_Click(object sender, EventArgs e)
{
FileInfo file = new FileInfo(@"E:\Cshap_text\PublishUpdate\PublishUpdate\bin\Debug\2.jpg");
string dtime = DateTime.Now.AddHours(-8).ToString("r");
StringBuilder httpHeader = new StringBuilder();
//构造http头
httpHeader.Append("PUT /2.jpg HTTP/1.1\r\n");
httpHeader.Append("Host:bucketdataupdate.oss-cn-beijing.aliyuncs.com\r\n");
httpHeader.Append("Content-Encoding:utf-8\r\n");
httpHeader.Append("Content-Disposition: attachment;filename=2.jpg\r\n");
httpHeader.Append("Date:" + dtime + "\r\n");
httpHeader.Append("Content-Type:image/jpg\r\n");
httpHeader.Append("Content-Length:" + file.Length + "\r\n");
//构造oss Authorization验证
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = Encoding.UTF8.GetBytes("我的key密码");
StringBuilder mk = new StringBuilder();
mk.Append("PUT\n");
string md5 = GetMD5HashFromFile(@"E:\Cshap_text\PublishUpdate\PublishUpdate\bin\Debug\2.jpg");
mk.Append(md5 + "\n");
mk.Append("image/jpg\n");
mk.Append(dtime + "\n");
mk.Append("\n");
mk.Append("/bucketdataupdate/2.jpg\n");
byte[] dataBuffer = Encoding.UTF8.GetBytes(mk.ToString());
byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
string base64 = Convert.ToBase64String(hashBytes);
httpHeader.Append("Authorization:OSS 我的keyID:" + base64 + "\r\n");
//头部构造完毕
httpHeader.Append("\r\n");
byte[] header = Encoding.UTF8.GetBytes(httpHeader.ToString());
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect("bucketdataupdate.oss-cn-beijing.aliyuncs.com", 80);
client.Send(header);
client.SendFile(@"E:\Cshap_text\PublishUpdate\PublishUpdate\bin\Debug\2.jpg");
byte[] buffer = new byte[1024];
client.Receive(buffer);
string msg = Encoding.UTF8.GetString(buffer);
}
这个msg 值是下面的 的东东 验证失败 我就没搞懂 我都是按照api做的啊 怎么会验证失败哦?什么地方写错了还是那个地方没有对 求指点
HTTP/1.1 403 Forbidden
Server: AliyunOSS
Date: Wed, 11 Nov 2015 03:40:50 GMT
Content-Type: application/xml
Content-Length: 810
Connection: keep-alive
x-oss-request-id: 5642B8C21FE4E74D7C6EDFA4
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<RequestId>5642B8C21FE4E74D7C6EDFA4</RequestId>
<HostId>bucketdataupdate.oss-cn-beijing.aliyuncs.com</HostId>
<OSSAccessKeyId>9Q619M9E5sL1ZtEb</OSSAccessKeyId>
<SignatureProvided>q880m3K4MezNpplFpk5N+7jyWHU=</SignatureProvided>
<StringToSign>PUT
image/jpg
Wed, 11 Nov 2015 03:40:49 GMT
/bucketdataupdate/2.jpg</StringToSign>
<StringToSignBytes>50 55 54 0A 0A 69 6D 61 67 65 2F 6A 70 67 0A 57 65 64 2C 20 31 31 20 4E 6F 76 20 32 30 31 35 20 30 33 3A 34 30 3A 34 39 20 47 4D 54 0A 2F 62 75 63 6B 65 74 64 61 74 61 75 70 64 61 74 65 2F 32 2E 6A 70 67 </StringToSignBytes>
</Error>
HTTP/1.1 400
-------------------------
-------------------------
-------------------------
-------------------------
-------------------------
-------------------------
-------------------------
-------------------------
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。