I am sending a post request to an api with c# webrequest and httpClient but always i get an error message from the api which says you posted invalid data in the header , but the point is when i send same data with chrome extension advanced rest client it works fine and i compared both request there is nothing different i have attached both request and my code , can anybody help to figure out what is the problem ,
this is the request from rest client app:
and here is the request from c#
and here is my c# code
string item = "<?xml version=\"1.0\" encoding=\"UTF - 8\"?>" +
"<request>" +
"<Username>admin</Username>" +
"<Password>" + password + "</Password>" +
"<password_type>4</password_type>" +
"</request> ";
var request = (HttpWebRequest)WebRequest.Create("http://192.168.8.1/api/user/login");
request.Method = "POST";
request.Headers["_RequestVerificationToken"]= Token;
request.Headers["Cookie"] = Sess;
byte[] bytes = Encoding.UTF8.GetBytes(item);
request.ContentType = "application/xml;charset=UTF-8";
request.ContentLength = bytes.Length;
Stream streamreq = request.GetRequestStream();
streamreq.Write(bytes, 0, bytes.Length);
streamreq.Close();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var result = reader.ReadToEnd();
}
The main conclusion is that one is not better than the other, and we shouldn't compare them since RestSharp is a wrapper around HttpClient. The decision between using one of the two tools depends on the use case and the situation.
HTTP client is a client that is able to send a request to and get a response from the server in HTTP format. REST client is a client that is designed to use a service from a server and this service is RESTful.
It looks like __RequestVerificationToken
contains two underscores in the left picture, so try this:
request.Headers["__RequestVerificationToken"]= Token;
The main difference it Saw here was that at the API you are sending
application/xml
but at the C# code you are sending
application/xml;charset=UTF-8;
In your API, the content length is 230 and in the C# the content length is 227, 3 chars less.
Now, It may be a long shot but charsets work differently with every browser and with every language so there might be an issue when you add charset=UTF-8
in your code.
Send your request as follows:
string item = "<?xml version=\"1.0\" encoding=\"UTF - 8\"?>" +
"<request>" +
"<Username>admin</Username>" +
"<Password>" + password + "</Password>" +
"<password_type>4</password_type>" +
"</request> ";
var request = (HttpWebRequest)WebRequest.Create("http://192.168.8.1/api/user/login");
request.Method = "POST";
request.Headers["_RequestVerificationToken"]= Token;
request.Headers["Cookie"] = Sess;
byte[] bytes = Encoding.UTF8.GetBytes(item);
request.ContentType = "application/xml";
request.ContentLength = bytes.Length;
Stream streamreq = request.GetRequestStream();
streamreq.Write(bytes, 0, bytes.Length);
streamreq.Close();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var result = reader.ReadToEnd();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With