C#httpwebrequest获取401 Unauthorized

时间:2012-09-13 09:17:38

标签: c# httpwebrequest http-status-code-401 unauthorized

try
{
     HttpWebRequest request = (Http WebRequest)WebRequest.Create("http://yigg.de/login");
     request.Method = "GET";
     request.Timeout = 10000;
     request.ReadWriteTimeout = 30000;
     request.ContentType = "application/x-www-form-urlencoded";
     request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
     request.Headers["Accept-Language"] = "en-us,en;q=0.5";
     request.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
     request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)";
     CredentialCache cc = new CredentialCache();
     cc.Add(new Uri("http://yigg.de/login"), "Basic", new NetworkCredential("user", "pass"));
     request.Credentials = cc;
     request.CookieContainer = container;
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     StreamReader reader = new StreamReader(response.GetResponseStream());
     string htmldoc = reader.ReadToEnd();
     response.Close();
     return htmldoc;
}
catch (Exception ex)
{
     return ex.Message;
}

我使用上面的代码,我总是得到“远程服务器返回错误:(401)未经授权”。请帮我。谢谢!

0 个答案:

没有答案