简体   繁体   English

邮差C#代码片段工作邮递员而不是应用程序

[英]Postman C# code snippet working on postman and not in app

I have an XML file that is sent as form-data to a remote server. 我有一个XML文件,作为表单数据发送到远程服务器。 When I use postman, I receive the response that I was supposed to receive. 当我使用邮递员时,我收到了应该收到的回复。

Postman response 邮递员的回应

Then I use the code snippet generator to port the call to my C# app, and when I run on the app it does not work. 然后我使用代码片段生成器将调用移植到我的C#应用​​程序,当我在应用程序上运行它不起作用。

C# response C#响应

The code snippet is: 代码段是:

var client = new RestClient("http://172.20.27.1/xxxxxx/xxxxxx");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "1d9df75d-2c8f-4c92-b861-9f4291145846");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Metas\"; filename=\"C:\\Users\\agufprad\\Desktop\\Pepillo\\FLEX_REMAN_CfgRec_Final_00-00-00-4D_20190311-102223_1010039188_7000AHB.xml\"\r\nContent-Type: application/xml\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

Is there anything I am not seeing here? 有什么我没看到的吗?

Can you export + upload the postman collection. 你能导出+上传邮递员收藏吗? That would help a lot. 这会有很大帮助。

A couple of things I see off the bat 我看到了一些关于蝙蝠的事情

1) You have a postman token header - why do you need this in C#? 1)你有一个邮差令牌头 - 为什么你需要在C#?

2) Does none multi-part data work? 2)没有多部分数据可以工作吗?

3) Are you sure content type is supposed to have the boundary data? 3)你确定内容类型应该有边界数据吗?

4) There is no file associated with the snippet. 4)没有与该代码段相关联的文件。 Pretty sure you can't just AddParameter multi-part data. 很确定你不能只是AddParameter多部分数据。 Look for AddFile , which may be your issue. 寻找AddFile ,这可能是你的问题。

5) Does an example off the web work? 5)网络上有一个例子吗? Currently this looks like it would be a malformed multi-part request 目前,这似乎是一个格式错误的多部分请求

Thanks to everyone for the responses, gave an idea of where to look. 感谢大家的回复,给出了一个在哪里看的想法。

Finally i added a header on the post method request.AddHeader("Accept", "text/plain"); 最后我在post方法request.AddHeader("Accept", "text/plain");上添加了一个标题request.AddHeader("Accept", "text/plain");

And added the file request.AddFile(fileName, Properties.Settings.Default.SaveRoute + fileName); 并添加了文件request.AddFile(fileName, Properties.Settings.Default.SaveRoute + fileName);

Thanks a lot! 非常感谢!

I would advice you remove any content-length param, postman-token, cookie param generated from the code generator on Postman, and it should work as its own specific request. 我建议你删除从Postman上的代码生成器生成的任何内容长度参数,邮递员令牌,cookie参数,它应该作为自己的特定请求。

Remove 去掉

 request.AddHeader("Postman-Token", "1d9df75d-2c8f-4c92-b861-9f4291145846");

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

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