Younis Qadir
Younis Qadir

Reputation: 435

Send Web request working with rest client app, but not working with c#

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:

enter image description here

and here is the request from c#

enter image description here

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

Upvotes: 7

Views: 1789

Answers (2)

Ulf Kristiansen
Ulf Kristiansen

Reputation: 1631

It looks like __RequestVerificationToken contains two underscores in the left picture, so try this:

request.Headers["__RequestVerificationToken"]= Token;

Upvotes: 4

Barr J
Barr J

Reputation: 10927

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

Upvotes: 1

Related Questions