Submit a web request that works with the client application for the rest, but does not work with C #

I am sending a mail request in api with C # webrequest and httpClient, but always get an error message from api saying that you have placed invalid data in the header, but the fact is that I am sending the same data with the chrome extension client for recreation, it works fine, and I compared both requests: there is nothing else that I attached both the request and my code, can someone help figure out what the problem is,

This is a request from the client application for rest:

enter image description here

and here is a 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();
}
+6
source share
2 answers

, __RequestVerificationToken , :

request.Headers["__RequestVerificationToken"]= Token;
+3

, , API

application/xml 

#

application/xml;charset=UTF-8;

API 230, # - 227, 3 .

, , - , , charset=UTF-8 .

:

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

All Articles