简体   繁体   English

为什么这个请求可以使用 HttpWebRequest 而不是 RestSharp?

[英]Why does this request works using HttpWebRequest but not with RestSharp?

I am consuming an API that expects a XML in the body request.我正在使用 API ,它期望在正文请求中出现 XML 。 First i consumed the api via Postman and it worked, then i used that tool of Postman to convert the request to RestCharp C# code, and then using that code the reponse that i was receiving was different compared to postman. First i consumed the api via Postman and it worked, then i used that tool of Postman to convert the request to RestCharp C# code, and then using that code the reponse that i was receiving was different compared to postman. After that, i used Fiddler to generate c# code with the postman request, and using that code that fiddler generated i was able to consume the API via code sucessfully.之后,我使用 Fiddler 使用 postman 请求生成 c# 代码,并使用提琴手生成的代码,我能够通过代码成功使用 API。 I am just trying to understand what is the difference between the code generated from postman and the code generated from Fiddler.我只是想了解从 postman 生成的代码和从 Fiddler 生成的代码之间有什么区别。

This is the code that is generated from Fiddler and it works:这是从 Fiddler 生成的代码,它可以工作:

            HttpWebRequest request = 
            (HttpWebRequest)WebRequest.Create("http://x.x.x.x.x");

            request.Accept = "*/*";
            request.KeepAlive = true;

            request.Method = "POST";
            request.ServicePoint.Expect100Continue = false;

            byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
            request.ContentLength = postBytes.Length;
            Stream stream = request.GetRequestStream();
            stream.Write(postBytes, 0, postBytes.Length);
            stream.Close();
            response = (HttpWebResponse)request.GetResponse();

This is the code generated from Postman (slightly altered, but still the code that was generated from postman didn't work and i don't think that the changes that was made interfered with the result) using RestSharp that doesn't work:这是从 Postman 生成的代码(略有改动,但仍然是从 postman 生成的代码不起作用,我认为所做的更改不会干扰结果)使用不起作用的 RestSharp:

        var client = new RestClient("http://x.x.x.x.x");

        client.ConfigureWebRequest((r) =>
        {
           r.ServicePoint.Expect100Continue = false;
           r.KeepAlive = true;
        });

        var request = new RestRequest();

        request.AddXmlBody(body);
        IRestResponse response = client.Post(request);
        return response;

I tried a lot of things in the RestSharp code, like adding a header with different content-types and encoding, for example我在 RestSharp 代码中尝试了很多东西,例如添加具有不同内容类型和编码的 header

     request.AddHeader("Content-Type", "text/xml;charset=utf-8");

but nothing worked.但没有任何效果。 The response from the api when consumed by the RestSharp code says it got an error of NPE, which i believe it means NullPointerException, but since the api is working just fine via postman and the code generated by Fiddler, i don't think the problem is in the API.当被 RestSharp 代码使用时,来自 api 的响应说它出现了 NPE 错误,我认为这意味着 NullPointerException,但是由于 api 通过 Z03D476861AFD38411A8AAZ 工作得很好,通过 Z03D476861AFD384510F2CB80 生成的问题和问题在 API 中。 Btw, the parameter body in the code are the exact same in both codes.顺便说一句,代码中的参数主体在两个代码中完全相同。

It looks like the request body is not matching with expected content type by API.看起来请求正文与 API 的预期内容类型不匹配。 When the content does not match the content type expected by the API then you may get NPE error.当内容与 API 预期的内容类型不匹配时,您可能会收到 NPE 错误。

in your fiddler generated code you are sending XML string as text.在您的提琴手生成的代码中,您将 XML 字符串作为文本发送。

Please add the following code:请添加以下代码:

request.AddHeader("Content-Type", "text/plain");
request.AddParameter("undefined", "<YourXml></YourXml>", ParameterType.RequestBody);

or或者

request.AddHeader("Content-Type", "application/xml");
request.AddParameter("undefined", "<YourXml></YourXml>", ParameterType.RequestBody);

Instead of代替

request.AddXmlBody(body); request.AddXmlBody(body);

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

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