简体   繁体   English

如何使用 C# HttpClient 发送未编码的表单数据

[英]How to send unencoded form data with C# HttpClient

I'm trying to "repurpose" a third-party API used by a desktop application.我正在尝试“重新利用”桌面应用程序使用的第三方 API。 I've found that the below code gets me very close to matching the packets sent by the app:我发现下面的代码让我非常接近匹配应用程序发送的数据包:

var formData = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>(JsonConvert.SerializeObject(myPayload), "")
});

var response = Client.PostAsync(myURL, formData).Result;
var json = response.Content.ReadAsStringAsync().Result;

This gets me almost exactly the same payload sent by the application, except it encodes the data (I know, "encoded" is right there in the name).这让我得到了应用程序发送的几乎完全相同的有效负载,除了它对数据进行编码(我知道,“编码”就在名称中)。 I need to get the exact same request but without the data being encoded, but I can't quite find the right object(s) to pull it off.我需要得到完全相同的请求,但没有对数据进行编码,但我找不到合适的对象来完成它。 How do I keep this payload from being URL encoded?如何防止此有效负载被 URL 编码?

Edit:编辑:

This is a login request I pulled from Wireshark emanating from the application:这是我从应用程序发出的 Wireshark 中提取的登录请求:

POST /Login HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: 1.1.1.1
Content-Length: 161
Expect: 100-continue
Connection: Close

{"username":"myuser","auth-id":"0a0a140f81a2ce0c303386e93cec41bf04660c22a881be9a"}

This is what the above will generate:这就是上面将生成的内容:

POST /Login HTTP/1.1
Expect: 100-continue
Connection: Close
Content-Type: application/x-www-form-urlencoded
Content-Length: 221
Host: 1.1.1.1

%7B%22user-name%22%3A%22myuser%22%2C%22auth-id%22%3A%220a0a140f81a2ce0c303386e93cec41bf04660c22a881be9a%22%7D=

I've edited them for brevity so the Content-Length is wrong.为了简洁起见,我对它们进行了编辑,因此 Content-Length 是错误的。 I realize it might not be the best way to send this data, but I have no control over how it's consumed.我意识到这可能不是发送这些数据的最佳方式,但我无法控制它的使用方式。

Since you're actually trying to send JSON, I think you need to wrap the JSON in a StringContent object rather than a FormUrlEncoded object. Since you're actually trying to send JSON, I think you need to wrap the JSON in a StringContent object rather than a FormUrlEncoded object. Form-encoded data and JSON data are two different ways of formatting a payload (another commonly used format would be XML, for example).表单编码数据和 JSON 数据是格式化有效负载的两种不同方式(例如,另一种常用的格式是 XML)。 Using them both together doesn't make any sense.两者一起使用没有任何意义。

I think something like this should work:我认为这样的事情应该有效:

var content = new StringContent(JsonConvert.SerializeObject(myPayload), Encoding.UTF8, "application/json");
var response = Client.PostAsync(myURL, content).Result;
var json = response.Content.ReadAsStringAsync().Result;

(PS the Content-Type: application/x-www-form-urlencoded header sent by the application appears to be misleading, since the request body clearly contains JSON. Presumably the receiving server is tolerant of this nonsense, or just ignores it because it's always expecting JSON.) (PS 应用程序发送的Content-Type: application/x-www-form-urlencoded header 似乎具有误导性,因为请求正文明确包含 JSON。大概接收服务器可以容忍这种废话,或者只是忽略它,因为它是总是期待 JSON。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM