我需要向关注网址
发出GET请求[ ~ ] $ curl -u duff:X https://subs.pinpayments.com/api/v4/sitename/subscribers/7388.xml
其中-u
是用户名,然后X
是密码。
如何使用WebRequest
?
请建议
答案 0 :(得分:2)
WebRequest class有凭据属性,您可以设置该属性:
WebRequest request = WebRequest.Create(uri);
request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
另一种可能性是使用支持WebClient class的custom 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 类可能更合适。