2012-07-06 126 views
0

我正在使用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; 
     } 
+0

停止使用真/假控制流。 – Henrik 2012-11-20 18:32:39

+0

你不会处理你的流,只是关闭它。 – Henrik 2012-11-20 18:33:04

+0

不要捕捉任何和所有异常,就像第一次返回false时一样。你也没有处理你的WebRequest。 – Henrik 2012-11-20 18:35:22

回答

0

你忘了把一个space焦炭-H过吗?

+0

不幸被那些重复之前等等。没有运气之后增加空间:-(。从Solr的附带没有错误消息返回要么只是一个400错误的请求,的InnerException为null。THX – Mikos 2012-07-06 02:57:29

+0

但为什么你还发布时,你有JSON STR已经把JSON的网址是什么? – Marcus 2012-07-06 03:09:12

+0

hmm..not肯定,使不同的。我试图分差组合。 – Mikos 2012-07-06 03:18:59

0

今天我遇到了同样的问题。

尝试这两个东西

1)请记住,你不能发送JSON字符串像

[{"A":"1","B":"0","C":"","D":"Washington"}] 

相反,你可能需要按摩JSON更像

[{"A":"1","B":"0","D":"Washington"}] 

Solr不喜欢空值。

2)这第二招,帮助(发送数据时,通过到Solr“卷曲”):尝试用两个双引号替换您的JSON字符串的所有双引号发送到Solr请求之前。

json = json.Replace(@"""", @"""""");

0

你的代码是不工作的reasong,是因为你在.NET中使用卷曲的语法。

cURL是发送和接收HTTP请求的可执行文件,.Net是编程应用程序的框架。

他们是不一样的。

,使其与你首先应该张贴到正确的URI的.Net工作,你需要设置正确的ContentType属性像这样:

var uri = "http://localhost:8080/solr/update/json"; 
using (var r = WebRequest.Create(uri)) 
{ 
    r.ContentType = "application/json"; 
    r.Method = "POST"; 
    using (var rs = r.GetRequestStream()) 
     rs.Write // your data 
    // get response 
    // return response data 
} 

这就是说,为什么在自己造成的痛苦?只需使用已经具有用于SolR操作的类型化API的SolR连接器!例如

https://code.google.com/p/solrnet/

但是,如果你不想使用,那么至少使用现代HTTP API像https://nuget.org/packages/RestSharp

1

如果插入,那么你将需要使用你的JSON元素添加和文档。您还需要添加一个提交,以便更新索引。您也可以从您的uri中删除参数,因为您可以将它们添加到Web请求对象中。最后,你应该在你的收集名称。

string json = "{\"add\":{\"doc\":{" 
    + "\"UUID\":\"d2a174e4-81d6-487f-b68d-392be5d3d47a\"," 
    + "\"Extension\":\".AVI\"," 
    + "\"VideoFileName\":\"Clip 1.avi\"}},"; 
    + "\"commit\":{}}"; 
string uri = "http://localhost:8080/solr/collection/update"; 

WebRequest request = WebRequest.Create(uri); 
request.ContentType = "application/json"; 
request.Method = "POST"; 
byte[] bytes = Encoding.ASCII.GetBytes(json); 
Stream stream = null; 

try { 
    request.ContentLength = bytes.Length; 
    stream = request.GetRequestStream(); 
    stream.Write(bytes, 0, bytes.Length); 
} 
catch { 
    return; 
} 
finally { 
    if (stream != null) { 
    stream.Close(); 
    } 
} 
System.Net.WebResponse response = request.GetResponse(); 
if (response == null) { 
    return; 
} 

如果插入多个对象到Solr你可以添加多个对象添加或文档对象的JSON。例如...

json = "{add:{doc:{keys:values}}, add:{doc:{keys:values}}, commit:{}}" 

json = "{add:{doc:{keys:values}, doc:{keys:values}}, commit:{}}" 

当你调试这款腕表的日志Solr的。它会提醒您任何可能发生的问题。

1

首先,我不认为你传递一个正确的JSON数据与-d选项再看看在你的代码下面的格式代码。

" -H 'Content-type:application/json' -d ' [ " + json + " ' ]" 

假设,你的JSON数据是{"name":"sam"}然后,上面的格式结果

-H 'Content-type:application/json' -d ' [{"name":"sam"} ' ] 

你传递一个JSON数据缺失。

除此之外,您在Solr的索引更新文件的做法是错误的。看看下面的简单代码。 [BTW:您可以通过在URL中的“承诺”的说法。

public async Task PostAsync() 
{ 
    string json = "{\"add\": {\"doc\": {" 
     + "\"id\":\"12345\"," 
     + "\"firstname\":\"Sam\"," 
     + "\"lastname\":\"Wills\"," 
     + "\"dob\":\"2016-12-14T00:00:00Z\"" 
     + "}}}"; 

    using (var client = new HttpClient()) 
    { 
     string uri = "http://localhost:8983/solr/people/update/json?wt=json&commit=true"; 
     var jsonContent = new StringContent(json); 
     await client.PostAsync(new Uri(uri), jsonContent); 
    } 
} 

如果要更新特定字段而不是整个文档[部分更新],请使用以下代码段。

public async Task PartialPostAsync() 
{ 
    string json = "{\"add\": {\"doc\": {" 
     + "\"id\":\"12345\"," 
     + "\"lastname\":{\"set\":\"George\"}" 
     + "}}}"; 

    using (var client = new HttpClient()) 
    { 
     string uri = "http://localhost:8983/solr/people/update/json?wt=json&commit=true"; 
     var jsonContent = new StringContent(json); 
     await client.PostAsync(new Uri(uri), jsonContent); 
    } 
} 

'id'字段是唯一字段。