Cowborg
Cowborg

Reputation: 2871

Simple POST from website to external web api fails

I have an asp .net MVC-page

Im trying to connect to Eventbrite:s api

Simply put, it requires you to send client id to one url, using HttpGET and HttpPOST the result and some more info to another url.

The GET goes fine and I get the required (auth)"code". When I try to make the POST to the second url I get

"Socket Exception: An existing connection was forcibly closed by the remote host"

I can POST to the second url, using Postman and the info from the GET-request it works ok, I get the auth token just fine.

This is the code Im using

    var parameters = new Dictionary<string,string>();
    parameters.Add("code", pCode);
    parameters.Add("client_secret", CLIENT_SECRET);
    parameters.Add("client_id", CLIENT_APP_KEY);
    parameters.Add("grant_type", "authorization_code");

    using (var client = new HttpClient())
    {
        var req = new HttpRequestMessage(HttpMethod.Post, pUrl) { Content = new FormUrlEncodedContent(parameters) };
        var response = client.SendAsync(req).Result;
        return response.Content.ReadAsStringAsync().Result;
    }

I have a vague memory of a similar problem when publishing to Azure. Since I have to register my app with a public return url I cant look at the request with fiddler.

My site is running https. I have also tested adding the below line (from some googling)

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

But then I get 404-error...

I have also tested this (with same result)

    using (var client = new HttpClient())
        {
            var response = client.PostAsync(pUrl, content).Result;                
            authToken = response.Content.ReadAsStringAsync().Result;
        }

Ive tested getting the auth code and and running the POST from local machine, same result...

I have contacted eventbrite developer support to see if they can help me as well...

Upvotes: 1

Views: 330

Answers (2)

Cowborg
Cowborg

Reputation: 2871

Changed OAuth access token URL from

to (as specified)

(ie without trailing slash). Now it works

Upvotes: 0

Joey Cai
Joey Cai

Reputation: 20127

This POST must contain the following urlencoded data, along with a Content-type: application/x-www-form-urlencoded header.

Since your content-type is application/x-www-form-urlencoded you'll need to encode the POST body, especially if it contains characters like & which have special meaning in a form. Then use the following function to post your data:

using (var httpClient = new HttpClient())
    {
        using (var content = new FormUrlEncodedContent(parameters))
        {
            content.Headers.Clear();
            content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            HttpResponseMessage response = await httpClient.PostAsync(url, content);

            return await response.Content.ReadAsAsync<TResult>();
        }
    }

The error message you provided means the remote side closed the connection, the causes are:

·You are sending malformed data to the application.

·The network link between the client and server is going down for some reason.

·You have triggered a bug in the third-party application that caused it to crash.

·The third-party application has exhausted system resources.

·Set ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

For more details, you could refer to this case.

Upvotes: 1

Related Questions