简体   繁体   中英

RestSharp issues when sending SOAP style Request

I recently implemented RestSharp instead of using plain old HTTP client in .NET Core. This was too help with some OAuth1 authentication problems and it worked great.

However, one requirement I need to support is now broken - a call to an old version of NAV that still uses a SOAP envelope and XML.

My RestSharp request is built like this (partially redacted):

var request = new RestRequest(transmission.Url.PathAndQuery, Helper.GetRestSharpMethod(transmission.Method), dataType);

request.AddParameter("text/xml", body, "text/xml", ParameterType.RequestBody);

Where dataType is Datatype.Xml and body is my soap envelope as XML text. I've tried "application/xml". I've also tried using request.AddBody and request.AddBodyXml - but they just caused more issues.

The endpoint (which we know works with the old request) returns an issue with being unable to read data from inside the SOAP envelope (which again we know is there, because it's in the parameters of the request).

Can anybody suggest any ways of fixing this issue and still using rest sharp? Or is this not really intended to be supported, and I'd be better off using plain HTTP Client for this specific part?

Thanks.

EDIT:

Concerning the old code, SOAP envelope text is identical. The difference is that instead of using RestSharp request.AddBody(), I built standard HttpRequestMessage, then set the HttpContent as new StringContent(content)

This was sent using standard .NET Core httpclient

var response = await client.SendAsync(request);

Ok finally got the to the bottom of this - posting here just in case anybody else has similar issues.

First of all - yes, it is supported.

  • However, there are a number of gotchas along the way - RestSharp automagically handles escaped URL strings. Don't escape them yourself.

  • Use request.AddParameter with ParameterType.RequestBody instead of request.AddBody.

  • When creating the rest client, be sure to give it the full URI when using basic authentication (I was using partial base address for OAuth1 , and carried it over).

  • When creating IRestRequest , be careful with supplying additional parameters like DataFormat , as removing this seemed to help resolve the problem.

Hope this helps.

I think this will solve the problem:

var client = new RestClient(requestUrl);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "text/xml");
request.AddParameter("text/xml", requestBody, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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