首页 新闻 搜索 专区 学院

远程服务器返回异常: (404) 未找到

0
悬赏园豆:10 [已解决问题] 解决于 2013-11-13 17:52

代码如下,res 赋不了值,为什么?

private HttpWebResponse GetData(string url, string agent = null, string prod_id = null, string oauth_consumerKey = "C16207CBF5444A5BB2499189D1E526D5", string oauth_consumerSecret = "8CC0EAFFED534E85AA4B7CE63784D4E2")
        {
            
            var oAuth = new CommonLib.OAuthBase();
            var oauth_nonce = oAuth.GenerateNonce();
            var oauth_timeStamp = oAuth.GenerateTimeStamp();
            Uri uri = new Uri(url);
            
            var authheader = oAuth.GenerateAuthorizationHeader(
                uri,
                oauth_consumerKey,
                oauth_consumerSecret,
                "GET",
                oauth_timeStamp,
                oauth_nonce
                );

            
            var req = WebRequest.Create(uri);
            req.Headers[HttpRequestHeader.Authorization] = authheader;

            if ("test3283".Equals(prod_id))
            {
                ((HttpWebRequest)req).AddRange(30,100);
            }
            
            if (agent != null)
            {
                ((HttpWebRequest)req).UserAgent = agent;
            }
            try
            {
                var res = req.GetResponse() as HttpWebResponse;
                return res;
            }
            catch (WebException ex)
            {
                var res = ex.Response as HttpWebResponse;
                return res;
            }

        }

菜鸟求教学的主页 菜鸟求教学 | 菜鸟二级 | 园豆:201
提问于:2013-11-12 15:58
< >
分享
最佳答案
0

最后 WebRequest.Create(uri);这个URL地址错误,断点看URL的值,应该是一个无效URL,不知道你GenerateAuthorizationHeader这个方法里把URL怎么处理了。可能后面的参数不对

收获园豆:10
Zery | 大侠五级 |园豆:6069 | 2013-11-12 16:16

断点看过了,是有效的啊,给你看看GenerateAuthorizationHeader的代码:、


 public string GenerateAuthorizationHeader(Uri url, string consumerKey, string consumerSecret, string httpMethod, string timeStamp, string nonce)
        {
            string nomalurl, param;
            var sig = GenerateSignature(url, consumerKey, consumerSecret, string.Empty, string.Empty, httpMethod, timeStamp, nonce, SignatureTypes.HMACSHA1, out nomalurl, out param);

            // AuthorizationHeader生成
            sig = HttpUtility.UrlEncode(sig);
            StringBuilder sb = new StringBuilder("OAuth realm=\"\"");
            sb.AppendFormat(",oauth_consumer_key=\"{0}\"", consumerKey);
            sb.AppendFormat(",oauth_nonce=\"{0}\"", nonce);
            sb.AppendFormat(",oauth_timestamp=\"{0}\"", timeStamp);
            sb.AppendFormat(",oauth_signature_method=\"{0}\"", "HMAC-SHA1");
            sb.AppendFormat(",oauth_version=\"{0}\"", "1.0");
            sb.AppendFormat(",oauth_signature=\"{0}\"", sig);

            return sb.ToString();
        }

菜鸟求教学 | 园豆:201 (菜鸟二级) | 2013-11-12 16:20

@菜鸟求教学: 

那你把生成的URL串直接到 WebRequest.Create(uri) 这个方法里执行下,就知道有没有问题了

Zery | 园豆:6069 (大侠五级) | 2013-11-12 16:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册