代码如下,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;
}
}
最后 WebRequest.Create(uri);这个URL地址错误,断点看URL的值,应该是一个无效URL,不知道你GenerateAuthorizationHeader这个方法里把URL怎么处理了。可能后面的参数不对
断点看过了,是有效的啊,给你看看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();
}
@菜鸟求教学:
那你把生成的URL串直接到 WebRequest.Create(uri) 这个方法里执行下,就知道有没有问题了