Neelesh Malvi

Neelesh Malvi

  • NA
  • 125
  • 0

ASP.net Web API Core

Feb 25 2019 8:37 AM
Hello everyone,
 
Need help in web api post request.
 
For below code i am getting 401 unauthorized error. But it works with POSTMAN. Can someone help me why is it failing via code. Below is a POST method.
 
using (StreamReader sr = new StreamReader(json))
{
string json = sr.ReadToEnd().Trim();
var request = (HttpWebRequest)WebRequest.Create("url");
var data = Encoding.ASCII.GetBytes(json);
request.ContentType= "application/json";
request.ContentLength=data.Length;
request.Headers.Add("RequestToken", XXX);
request.Headers.Add("Key", XXX);
request.Headers.Add("Client", XXX);
request.Headers.Add("TimeStamp", XXX);
request.Method = "POST";
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();// at this stage I am getting "'The remote server returned an error: (401) Unauthorized.'"
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
 

Answers (1)