用C#编写的卷曲请求不起作用

时间:2016-01-26 16:05:56

标签: c# .net

我试图在C#中编写特定的curl请求,并且我不断从服务器获得500服务器错误响应。此curl请求实质上是由Highwinds公司向API发出请求。此请求发送json数据,并设置Auth Bearer令牌标头。

这是正常的卷曲请求(请注意,我已使用{token}替换了我的实际持票人令牌,并使用{accountId}替换了我的实际帐户ID来混淆该信息):

curl -H "Authorization: Bearer {token}" -H "Content-Type: application/json" -d "@data.json" "https://striketracker.highwinds.com/api/accounts/{accountId}/purge"

这里是C#代码,它给我一个来自Highwinds API的通用500服务器错误(请注意,我已使用{token}替换了我的实际持票人令牌,我的实际帐户ID为{accountId},并使用{url}在json字符串中的url,以便混淆该个人信息):

var accountId = "{accountId}";
var purgeURI =  string.Format("https://striketracker.highwinds.com/api/accounts/{0}/purge", {accountId});
var query =
@"{""list"": [{""url"": ""{url}"",""recursive"": true}]}";
var token = {token};


using (var httpClient = new HttpClient())
{

var url = new Uri(purgeURI);
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url))
{
    httpRequestMessage.Headers.Add(System.Net.HttpRequestHeader.Authorization.ToString(),
      string.Format("Bearer {0}", token));

    httpRequestMessage.Content = new StringContent(query,
                Encoding.UTF8,
                "application/json");

    await httpClient.SendAsync(httpRequestMessage).ContinueWith(task =>
    {

        var response = task.Result;
        var blah = response.Content.ReadAsStringAsync().Result;

        Console.WriteLine(response.Content.ReadAsStringAsync().Result);
    });
}
}

谢谢!

*更新:添加了以下代码行,以删除HttpRequest默认添加到请求的Expect标头。删除此标题后,我能够让Highwinds API接受请求而不会被轰炸。

" request.ServicePoint.Expect100Continue = false;"

1 个答案:

答案 0 :(得分:1)

我最好的建议是通过 tcpmon http://archive.apache.org/dist/ws/tcpmon/1.0/之类的代理来代理这两个请求(基本上运行服务器并指向本地主机并让tcpmon将请求重定向到striketracker.highwinds.com )。从curl和源代码处尝试,您应该能够看到请求之间的不同。

相关问题