Differences between using C# HttpClient API and the postman testing? Client call works on postman, but not C# httpClient getAsync

api call works from postman but not httpwebrequest
api working in postman but not in app c#
post request works in postman but not in browser
httpclient c#
request working in postman but not in curl
convert postman to c#
c# rest api
c# httpclient post json

I am testing a REST API post, and it works well when I try it on Postman. However, in some scenario (related to the posting XML data) if I post with HttpClient API, I would receive the following error:

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

But the same XML content works fine on Postman with status OK and proper response.

What is the differences between using the C# HttpClient API and the postman testing? How can I configure my API call to match with the behavior on postman?

Here I attached the source code, and the Postman screenshot

public void createLoan()
    string baseCreateLoanUrl = @"https://serverhost/create?key=";
    var strUCDExport = XDocument.Load(@"C:\CreateLoan_testcase.xml");

    using (var client = new HttpClient())
        var content = new StringContent(strUCDExport.ToString(), Encoding.UTF8, Mediatype);
        string createLoanApi = string.Concat(baseCreateLoanUrl, APIKey);

            var response = client.PostAsync(createLoanApi, content).Result;
        catch (Exception ex)
            MessageBox.Show("Error Happened here...");

        if (response.IsSuccessStatusCode)
            // Access variables from the returned JSON object
            string responseString = response.Content.ReadAsStringAsync().Result;
            JObject jObj = JObject.Parse(responseString);

            if (jObj.SelectToken("failure") == null)
                // First get the authToken
                string LoanID = jObj["loanStatus"]["id"].ToString();
                MessageBox.Show("Loan ID: " + LoanID);
                string getTokenErrorMsg = string.Empty;

                JArray errorOjbs = (JArray) jObj["failure"]["errors"];
                foreach (var errorObj in errorOjbs)
                    getTokenErrorMsg += errorObj["message"].ToString() + Environment.NewLine;

Thanks for Nard's comment, after comparing the header, I found the issue my client header has this: Expect: 100-continue

While postman doesn't has.

Once I removed this by using the ServicePointManager:

ServicePointManager.Expect100Continue = false;

Everything seems fine now. Thanks all the input!

Working With a REST API Using HttpClient, We can use POSTMAN to test or generate the access token. In this article, I am going to use two different methods: EmployeeRegisteration� C# HttpClient. In this article, you will learn how to call Web API using HttpClient in ASP.NET. HttpClient class provides a base class for sending/receiving the HTTP requests/responses from a URL. It is a supported async feature of .NET framework. HttpClient is able to process multiple concurrent requests. It is a layer over HttpWebRequest and HttpWebResponse. All methods with HttpClient are

My gut tells me it's something simple. First, we know the API works, so I'm thinking it's down to how you are using the HttpClient.

First things first, try as suggested by this SO answer, creating it as a singleton and drop the using statement altogether since the consensus is that HttpClient doesn't need to be disposed:

    private static readonly HttpClient HttpClient = new HttpClient();

I would think it would be either there or an issue with your content encoding line that is causing issues with the API. Is there something you are missing that it doesn't like, I bet there is a difference in the requests in Postman vs here. Maybe try sending it as JSON ala:

     var json = JsonConvert.SerializeObject(strUCDExport.ToString());
     var content = new StringContent(json, Encoding.UTF8, Mediatype);

Maybe the header from Postman vs yours will show something missing, I think the real answer will be there. Have fiddler running in the background, send it via Postman, check it, then run your code and recheck. Pay close attention to all the attribute tags on the header from Postman, the API works so something is missing. Fiddler will tell you.

C# Web Api problem, C# Web Api problem - test works well, requesting from browser (via Postman or otherwise) fails. processor) { using (var client = new HttpClient()) { client. Clearly there is a problem with calling this type of process from my localhost I'm comfortable with doing my own research, but I'm not sure what to look for in this case� For more information, see Writing Web API Client Code for Multiple Platforms Using Portable Libraries. NOTE: If you pass base URLs and relative URIs as hard-coded values, be mindful of the rules for utilizing the HttpClient API. The HttpClient.BaseAddress property should be set to an address with a trailing forward slash (/).

I was struggling with this for 2 days when I stumbled over Fiddler which lets you record the traffic to the service. After comparing the calls I saw that I had missed a header in my code.

How To Consume RestAPI Using HttpClient In C#, ReadAsAsync; PostAsync; PutAsync; GetAsync; SendAsync etc. In this article, I used HttpClient to Consume RestAPI Services. We can use POSTMAN tool to test or generate the access token. In In order to work with the above methods, each method contains URL Implement JWT In .NET Core API. 08. Moq therefore has an API for that. You do using Moq.Protected; in your using clauses, and then you can go on on your Moq with the .Protected() method. This gives you some additional methods on the Moq, where you can access the protected members using their names. A complete test of a class using a HttpClient using Moq would look like this:

REST Api GetAsync method : Control doesnt , But could you please let me know how do you call the GetAsync() after this HttpClient client = new HttpClient(); var url = String. I send this request "http:// localhost:5000/api/weather/city/test" in postman to request below code: In Postman it works, but when you debug the code using visual studio and� We've also created the Postman Community Forum as a place for our community to talk to each other and help each other out with questions. Whether you're new to Postman or a seasoned power user, the forum is a great place to post questions and share ideas on a variety of API development topics with fellow Postman users and the Postman team.

Simple C# .NET 4.5 HTTPClient Request Using Basic Auth and , client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers. performance test results are much in favor of static initialization of the HttpClient. Hi , I want to call the third party SMS APi call - TransmitSMS from my .net web Not sure what gives but for what ever reason creating the task and running it in� In Xamarin or other .NET Standard based platform we use the HttpClient class in order to do HTTP calls and JSON.NET to deserialize the response. In this post we will see how to improve our code so as to to make efficient api calls.

2020 07 axj5jo restsharp vs postman, In Postman it works, but when you debug the code using visual studio and put a HTTP client that you can use in all of your applications. rest-api api postman asked Jul I did a quick performance test to find how WebClient (Sync calls), HttpClient the process of creating a RESTful Webservice with C# and Visual Studio. Here Mudassar Ahmed Khan has explained with an example, how to call Web API 2 Controller method using HttpClient class in ASP.Net with C# .Net. The Web API 2 Controller method will fetch records from database using Entity Framework in ASP.Net MVC and later it will be called (consumed) in ASP.Net Web Forms (ASPX) page using HttpClient class in ASP.Net. TAGs: ASP.Net, GridView, MVC, Web API

  • can u add your contract (interface) on ur post ?
  • Sorry @ahankendi , unfortunately, I don't have access to the server side api.
  • Although It probbably doesn't have much to do with your issue, please stop using .Result and start using await instead. .Result will block your thread and can cause a deadlock if you are not being careful. I would suggest learning how to work with async/await, which will make your life so much easier. :)
  • Thanks for your comment, Kerim. I actually tried everything including async/await and even singleton like Nard suggested below before but they all return the same error.
  • In case someone else stumbles upon this question, what fixed my problem was setting the allowed security protocol: System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; courtesy of: stackoverflow.com/a/46223433/1243268
  • Thanks Nard for your detailed suggestion. However, now when I haved the fiddler running aside. I don't have the error as before, instead I got the "A loan already exists with the Loan Number: "1612EM000064", which sounds like the api has two shots and I got the 2nd response... why does fiddler would cause this?
  • resolved after comparing the header line by line using fiddler, removing "Expect: 100-continue" did the work. Check my own post for details.