我有一个asp .net MVC页面
我正在尝试连接到Eventbrite:s api
简而言之,它要求您使用HttpGET和HttpPOST结果将客户端ID发送到一个URL,并将更多信息发送到另一个URL。
GET正常,我得到了所需的(身份验证)“代码”。当我尝试将POST传递到第二个URL时
“套接字异常:现有连接被强制关闭 远程主机”
我可以使用Postman和来自GET-request的信息将其发布到第二个URL,它可以正常工作,我可以正常获得auth令牌。
这是我使用的代码
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;
}
发布到Azure时,我对类似的问题记忆犹新。由于我必须使用公共返回URL注册我的应用程序,因此无法使用提琴手查看请求。
我的网站正在运行https。 我还测试了添加以下行(通过谷歌搜索)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
但随后出现404错误...
我也对此进行了测试(结果相同)
using (var client = new HttpClient())
{
var response = client.PostAsync(pUrl, content).Result;
authToken = response.Content.ReadAsStringAsync().Result;
}
我已经测试过获取身份验证代码并从本地计算机运行POST,结果相同...
我已经联系了eventbrite开发人员支持,以查看他们是否也可以帮助我...
答案 0 :(得分:1)
此POST必须包含以下urlencoded数据以及Content-type:
application/x-www-form-urlencoded
标头。
由于您的内容类型为application/x-www-form-urlencoded
,因此您需要对POST正文进行编码,尤其是当它包含诸如&
之类的具有特殊含义的字符时。
然后使用以下函数发布您的数据:
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>();
}
}
您提供的错误消息表示远程端已关闭连接,原因是:
·您正在将格式错误的数据发送到应用程序。
·客户端和服务器之间的网络链接由于某种原因而断开。
·您已在第三方应用程序中触发了一个导致崩溃的错误。
·第三方应用程序耗尽了系统资源。
·设置ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
有关更多详细信息,您可以参考此case。
答案 1 :(得分:0)
更改了
的OAuth访问令牌URL至(指定)
(即不带斜杠)。现在可以了