I have a C# application which needs to call a WooCommerce API and read its HTML response. I use this code:
public static String code(string Url)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url);
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
return result;
}
but I get this error on the selected line:
The remote server returned an error: (401) Unauthorized.
I tried several ways but like every time I got the same error; these are some ways I tried to solve it:
myRequest .UseDefaultCredentials = true;
myRequest .PreAuthenticate = true;
myRequest .Credentials = CredentialCache.DefaultCredentials;
or
request.Credentials = new NetworkCredential("UserName", "PassWord");
request.UseDefaultCredentials = true; request.PreAuthenticate = true;
In addition, I should say that when I open my URL in the browser for the first time, Chrome shows me a security alert, but just for the first time. Please help me, I really need to solve this.