【发布时间】:2012-07-06 02:36:17
【问题描述】:
我正在通过 HTTP/POST 从 C#/Winforms/.NET4.0 应用程序使用 JSON 向 Solr 写入数据,以加快索引和使用以下代码。我向 solr 写了一个文档(基于这些 instructions),但不断收到“400 错误请求”。 JSON 看起来很干净,没有问题。
这似乎是一个语法问题,但过去几个小时我一直在努力解决这个问题,但无济于事。关于什么是错误的任何想法?感谢所有帮助。
这里是发布的 URI 字符串
"http://localhost:8080/solr/update/json -H 'Content-type:application/json' -d ' [ {\"UUID\":\"d2a174e4-81d6-487f-b68d-392be5d3d47a\",\"Extension\":\".AVI\",\"VideoFileName\":\"Clip 1.avi\"} ' ]"
string uri = http://localhost:8080/solr/update/json;
public bool WriteJSONToSolr(string uri, string json)
{
WebRequest request = WebRequest.Create(uri + " -H 'Content-type:application/json' -d ' [ " + json + " ' ]" );
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(json);
Stream stream = null;
try
{ // send the Post
request.ContentLength = bytes.Length; //Count bytes to send
stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length); //Send it
}
catch
{
return false;
}
finally
{
if (stream != null)
{
stream.Close();
}
}
System.Net.WebResponse response = request.GetResponse();
if (response == null) return false;
return true;
}
【问题讨论】:
-
停止使用真/假作为控制流。
-
你没有处理你的流,只是关闭它。
-
不要捕获任何和所有异常,就像您第一次返回 false 时所做的那样。你也没有处理你的 WebRequest。