C#使用HttpWebRequest请求url出现远程服务器返回一个错误:401未经授权
作者&投稿:诸弯 (若有异议请与网页底部的电邮联系)
服务器没有访问权限导致的,设置服务器的匿名访问即可
header呢?一般都要加一些身份验证在header里面。
怎么写你自己搜下
该写什么值建议你抓一下手动访问时候的包,或者看官方api说明
/*
* Created by SharpDevelop.
* User: Austin
* Date: 2015/10/16
* Time: 10:52
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Net;
using System.Text;
namespace nezha_api_test
{
public class Hello
{
private static string username = "***********";
private static string password = "**************";
public static void Main(string[] args)
{
try {
string url = "**************";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Credentials = GetCredentialCache(url, username, password);
request.Headers.Add("Authorization", GetAuthorization(username, password));
request.Method = "GET";
HttpWebResponse myResponse = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string s = reader.ReadToEnd();
reader.Close();
myResponse.Close();
Console.WriteLine(s);
Console.Read();
} catch (WebException e) {
StreamReader reader = new StreamReader(e.Response.GetResponseStream(), Encoding.UTF8);
string s = reader.ReadToEnd();
Console.WriteLine(s);
Console.Read();
}
}
#region # 生成 Http Basic 访问凭证 #
private static CredentialCache GetCredentialCache(string uri, string username, string password)
{
string authorization = string.Format("{0}:{1}", username, password);
CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
return credCache;
}
private static string GetAuthorization(string username, string password)
{
string authorization = string.Format("{0}:{1}", username, password);
return "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization));
}
#endregion
}
}
要先登录,保持COOKIE再访问你上面的地址。
蒋卢治必: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL); req.Timeout = 15000; //超时时间 req.CookieContainer = new CookieContainer(); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; ....
霍林郭勒市15888637758: C#模拟登录网站的cookie问题使用httpwebreques ?
蒋卢治必: 1. post登录之后获得CookieContainer,以后每次get的时候用这个CookieContainer登录post方法大致如下public static string SendData(string method, string Url, byte[] ...
霍林郭勒市15888637758: c#httpwebresponse接收到数据怎么使用数据 - ?
蒋卢治必: 问题是,你要用哪部分数据干什么?HttpWebResponse返回的数据,直接放到浏览器中可以解析出整个网页.
霍林郭勒市15888637758: C#获取访问网页Json数据 - ?
蒋卢治必: 用WebClient下载Json数据,然后用Iron的一个Json类库解析,然后得到结果. 如果用JS,那就更简单了.
霍林郭勒市15888637758: C#如何获得HTTP响应的数据包??? - ?
蒋卢治必: System.Net.WebClient n = new System.Net.WebClient();byte[] a = n.DownloadData("http://www.baidu.com/"); // 返回每个字符的ASCII码string b = n.DownloadString("http://www.baidu.com/"); // 返回HTML串n.DownloadFile("http://www.baidu.com/","c:\\a.txt"); // 把返回的HTML串保存到c:\a.txt中
霍林郭勒市15888637758: C #里的request如何获取数据库中表中的记录 - ?
蒋卢治必: 首先需要说明,在C#中REQUST有两种.1. 位于System.Web.HttpRequest是封装浏览器对服务器的请求的,主要用在ASP.NET中,其中包括浏览器请求的网址,查询字符串数据或表单数据等等.所以一般将System.Web.HttpReques中的Request通...
霍林郭勒市15888637758: C#中 查询方法 出错 求指教 - ?
蒋卢治必: 不要用(int) 来强行转换.建议你用 int.Parse(reader["user_sex"]). 比较好是:int s;if(int.tryparse(reader["user_sex"], out s)){ // 给错误讯息}userinfo...
霍林郭勒市15888637758: 我想用建立一个网页,接收远程GPRS传回的数据,能否用socket通信呢?打算用C#写 - ?
蒋卢治必: 网页的话可以直接用http通信 有get,post两种方法 网页接收数据的代码:get: Request.QueryString["data"] post:Request["data"].ToString();