开发者社区> 问答> 正文

.net直接用HttpWebRequest访问OSS总是报403返回码

            Uri uri = new Uri("xxxx");
            HttpWebRequest httpRequest = HttpWebRequest.Create(uri) as HttpWebRequest;
            httpRequest.Method = "GET";

            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("GET\n\n\n");
            String gmtTime = ToGMTFormat(DateTime.UtcNow);
            strBuilder.Append(gmtTime);
            strBuilder.Append("\n\n");
            strBuilder.Append("/yszmetadata/firstobject");
            String signatureStr = strBuilder.ToString();
            String signature = ComputeSignature(AccessKeySecret, strBuilder.ToString());
            String authorString = "OSS "   AccessKeyID   ":"   signature;
            httpRequest.Headers.Add("Authorization", ReEncode(authorString, "utf-8", "iso-8859-1"));
        
             MethodInfo mf  = typeof(WebHeaderCollection).GetMethod("AddInternal", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(string) }, null);
             mf.Invoke(httpRequest.Headers, new object[] { "Date", ReEncode(gmtTime, "utf-8", "iso-8859-1") });

            WebResponse webRespones = httpRequest.GetResponse();
上面的代码有和问题,AccessKeyID和AccessKeySecret都是对的,用.net sdk可以正确访问,直接用HttpWebRequest就不行,不知哪个地方没写好

展开
收起
zlb 2013-03-15 00:14:41 8316 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载