我想使用c#(xml)向索引添加文檔,但是我總是收到錯誤400(錯誤的請求)。任何想法我做錯了什麼?使用c#添加文檔
代碼:
private static string GetXml()
{
XDocument document = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("add",
new XElement("doc",
new XElement("field",
new XAttribute("name", "employeeId"),
new XText("05991")),
new XElement("field",
new XAttribute("name", "skills"),
new XText("Perl"))
)
)
);
return document.ToString(SaveOptions.DisableFormatting);
}
private static void AddDocument()
{
string content = GetXml();
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://mysolrhost:8080/solr/update");
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
byte[] byteArray = Encoding.UTF8.GetBytes(content);
request.ContentLength = byteArray.Length;
using (var requestStream = request.GetRequestStream())
using (var sw = new StreamWriter(requestStream))
{
sw.Write(content);
}
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse) response).StatusDescription);
}
public static void Main(string[] args)
{
AddDocument();
}
編輯:問題解決(見下面的答案)。
非常感謝!
可能想看看(例如)小提琴手中的http流量,看看導線上發生了什麼 – 2010-08-11 08:05:25