1

我正在使用 WebClient 类对 Elastic Search 的 JSON 字符串执行 HTTP“PUT”。此代码在 95% 的情况下有效,但在其余情况下失败。我得到的错误是我的代码中的 400 bad protocol。当我使用 jQuery AJAX 或名为 Postman 的 Chrome 扩展程序发布相同的 JSON 时,它可以完美运行。我不确定如何解决此错误。我的代码如下 -

string json = "{some json}";
WebClient client = new WebClient();
client.Headers["Content-Type"] = "application/json;charset=UTF-8";    

try
{
     json = client.UploadString(URL, "PUT", json);
}
catch (WebException ex)
{
     //catch exception
}
4

1 回答 1

2

Elastic Search expects some headers which WebClient does not pass. I used HttpWebRequest class and it worked fine without any errors since.

于 2012-07-11T14:10:55.217 回答