我正在使用JSON通过HTTP/POST从C#/ Winforms/.NET4.0应用写入Solr,以加快索引和使用下面的代码。我写了一个文件solr(基于这些instructions),但不断收到'400错误的请求'。 JSON似乎很干净,没有问题。通过HTTP/Post将JSON写入Solr
这似乎是一个语法问题,但我一直在摔跤这最后几个小时无济于事。关于什么是错误的任何想法?所有帮助赞赏。
这里是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;
}
停止使用真/假控制流。 – Henrik 2012-11-20 18:32:39
你不会处理你的流,只是关闭它。 – Henrik 2012-11-20 18:33:04
不要捕捉任何和所有异常,就像第一次返回false时一样。你也没有处理你的WebRequest。 – Henrik 2012-11-20 18:35:22