C#webrequest卷曲

时间:2014-03-22 19:07:23

标签: c# asp.net

我需要向关注网址

发出GET请求
[ ~ ] $ curl -u duff:X https://subs.pinpayments.com/api/v4/sitename/subscribers/7388.xml

其中-u是用户名,然后X是密码。

如何使用WebRequest

请建议

1 个答案:

答案 0 :(得分:2)

WebRequest class凭据属性,您可以设置该属性:

WebRequest request = WebRequest.Create(uri);
request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

另一种可能性是使用支持WebClient classcustom credentials

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
//Byte[] pageData = client.DownloadData(url);
//string pageHtml = Encoding.ASCII.GetString(pageHtml);
// or DownloadString: http://msdn.microsoft.com/en-us/library/fhd1f0sw%28v=vs.110%29.aspx
var pageHtml = client.DownloadString(uri);
Console.WriteLine(pageHtml);

如果您需要为请求设置自定义header information,则 WebClient 类可能更合适。