简体   繁体   English

使用rest客户端应用程序发送Web请求,但不使用c#

[英]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 , 我正在通过c#webrequest和httpClient向api发送一个帖子请求,但我总是从api收到一条错误消息,说明你在标题中发布了无效数据,但重点是我用chrome扩展高级休息客户端发送相同的数据工作得很好,我比较了两个请求没有什么不同,我已经附加了请求和我的代码,任何人都可以帮助找出问题所在,

this is the request from rest client app: 这是来自其他客户端应用程序的请求:

在此输入图像描述

and here is the request from c# 这是来自c#的请求

在此输入图像描述

and here is my c# code 这是我的c#代码

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

看起来__RequestVerificationToken__RequestVerificationToken包含两个下划线,所以试试这个:

request.Headers["__RequestVerificationToken"]= Token;

The main difference it Saw here was that at the API you are sending 它在这里看到的主要区别在于您发送的API

application/xml 

but at the C# code you are sending 但是你发送的C#代码

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. 在您的API中,内容长度为230,在C#中,内容长度为227,3个字符更少。

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. 现在,这可能是一个很长的镜头,但字符集对每个浏览器和每种语言的工作方式都不同,因此在代码中添加charset=UTF-8时可能会出现问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单的http Web发布请求无法通过c#代码正常运行,而来自REST客户端的相同内容可以正常运行 - Simple http web post request not working properly from c# code, whereas same content from REST client is working fine c#中的HTTPS GET REST请求不能与RestSharp一起使用 - HTTPS GET REST request in c# is not working with RestSharp 如何将参数从Advanced Rest Client(Chrome应用)发送到用C#编写的REST服务(asmx文件)? - How to send parameters from Advanced Rest Client(Chrome app) to REST service (asmx file) written in C#? 使用邮递员到 web api 2 c# 放置请求不起作用 - Put request is not working using postman to web api 2 c# C# - 无法让这个看似简单的 Web 请求正常工作 - C# - Can't get this seemingly simple web request working Azure ML Web服务请求在C#中不起作用 - Azure ML Web Service request not working in C# C#服务器和客户端无法正常工作 - C# server and client not working REST POST请求不适用于java但适用于REST客户端 - REST POST request not working on java but works on REST client 如何配置C#Web服务客户端以并行发送HTTP请求标头和正文? - How do I configure a C# web service client to send HTTP request header and body in parallel? C#使用禁用的JavaScript发送Web请求 - C# Send web request with disabled javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM