DotnetSparrow
DotnetSparrow

Reputation: 27996

C# webrequest to curl

I need to make a GET request to following url

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

where -u is username and then X is password.

How to use WebRequest?

Please suggest

Upvotes: 0

Views: 200

Answers (1)

keenthinker
keenthinker

Reputation: 7830

The WebRequest class has a Credentials property, which you can set:

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

Another possibility would be to use the WebClient class, that supports custom credentials too:

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);

If you need for a reason to set custom header information for the request, then the WebClient class could be more suitable.

Upvotes: 2

Related Questions