我将以下内容发布到 Solr 服务器:
<add>
<doc>
<field name="uniqueid">5453543</field>
<field name="modifieddate">2008-12-03T15:49:00Z</field>
<field name="title">My Record</field>
<field name="description">Descirption
Details
</field>
<field name="startdate">2009-01-21T15:26:05.680Z</field>
<field name="enddate">2009-01-21T15:26:05.697Z</field>
<field name="Telephone_number">11111 111 111(text phone)</field>
<field name="Telephone_number">11111 111 111</field>
<field name="Mobile_number">1111111111</field>
</doc>
</add>
我正在使用SolrNet发送文档,这里是代码的摘录(s 是上面的 xml):
public string Post(string relativeUrl, string s)
{
var u = new UriBuilder(serverURL);
u.Path += relativeUrl;
var request = httpWebRequestFactory.Create(u.Uri);
request.Method = HttpWebRequestMethod.POST;
request.KeepAlive = false;
if (Timeout > 0)
request.Timeout = Timeout;
request.ContentType = "text/xml; charset=utf-8";
request.ContentLength = s.Length;
request.ProtocolVersion = HttpVersion.Version10;
try
{
using (var postParams = request.GetRequestStream())
{
postParams.Write(xmlEncoding.GetBytes(s), 0, s.Length);
using (var response = request.GetResponse())
{
using (var rStream = response.GetResponseStream())
{
string r = xmlEncoding.GetString(ReadFully(rStream));
//Console.WriteLine(r);
return r;
}
}
}
}
catch (WebException e)
{
throw new SolrConnectionException(e);
}
}
当它到达 request.GetResponse 失败并出现以下错误:
base {System.InvalidOperationException} = {“远程服务器返回错误:(500)内部服务器错误。”}
当我在 apache 日志中查看服务器时,它给出了以下原因:
输入块意外结束
这是完整的堆栈跟踪:
2009 年 9 月 17 日上午 10:13:53 org.apache.solr.common.SolrException 日志严重:com.ctc.wstx.exc.WstxEOFException:输入块在 [row,col {unknown-source} 的结束标记中意外结束]:[26,1266] 在 com.ctc.wstx.sr.StreamScanner.loadMoreFromCurrent(StreamScanner.java:1054) 在 com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOB(StreamScanner.java:700) 在 com.ctc。 wstx.sr.StreamScanner.getNextCharFromCurrent(StreamScanner.java:811) 在 com.ctc.wstx.sr.BasicStreamReader.readEndElem(BasicStreamReader.java:3211) 在 com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java: 2832) 在 com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1019) 在 org.apache.solr.handler.XmlUpdateRequestHandler.processUpdate(XmlUpdateRequestHandler.java:148) 在 org.apache.solr.handler。XmlUpdateRequestHandler.handleRequestBody(XmlUpdateRequestHandler.java:123) 在 org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:131) 在 org.apache.solr.core.SolrCore.execute(SolrCore.java:1204) 在 org .apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:303) 在 org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:232) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain .java:235) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 在 org.apache.catalina 的 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)。 core.StandardContextValve.invoke(StandardContextValve.java:191) 在 org.apache.catalina.core。StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 在 org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859) 在 org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process (Http11AprProtocol.java:574) 在 org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1527) 在 java.lang.Thread.run(Thread.java:619)109) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 在 org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859) 在 org.apache.coyote.http11.Http11AprProtocol $Http11ConnectionHandler.process(Http11AprProtocol.java:574) at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1527) at java.lang.Thread.run(Thread.java:619)109) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 在 org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859) 在 org.apache.coyote.http11.Http11AprProtocol $Http11ConnectionHandler.process(Http11AprProtocol.java:574) at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1527) at java.lang.Thread.run(Thread.java:619)
请注意 Solr 服务器在以下系统上运行:
Microsoft Windows Server 2003 R2 Apache Tomcat 6
最后这是我的问题:
我发送的 Xml 对我来说看起来不错。有没有人知道为什么 Solr 会抛出这个异常?
谢谢
戴夫
编辑答案如下:
public string Post(string relativeUrl, string s)
{
var u = new UriBuilder(serverURL);
u.Path += relativeUrl;
var request = httpWebRequestFactory.Create(u.Uri);
request.Method = HttpWebRequestMethod.POST;
request.KeepAlive = false;
if (Timeout > 0)
request.Timeout = Timeout;
request.ContentType = "text/xml; charset=utf-8";
request.ProtocolVersion = HttpVersion.Version10;
try
{
// Set the Content length after the size of the byte array has been calculated.
byte[] data = xmlEncoding.GetBytes(s);
request.ContentLength = s.Length;
using (var postParams = request.GetRequestStream())
{
postParams.Write(data, 0, data.Length);
using (var response = request.GetResponse())
{
using (var rStream = response.GetResponseStream())
{
string r = xmlEncoding.GetString(ReadFully(rStream));
//Console.WriteLine(r);
return r;
}
}
}
}
catch (WebException e)
{
throw new SolrConnectionException(e);
}
}