C# 带C的Opensearchserver API#
我正在开发一个winforms c#应用程序,希望在我的应用程序中使用openSearchServer API。 您能否提供一些示例,说明如何获取搜索的JSON/XML响应? 我目前正在使用以下方法,但可以提高效率C# 带C的Opensearchserver API#,c#,opensearch,C#,Opensearch,我正在开发一个winforms c#应用程序,希望在我的应用程序中使用openSearchServer API。 您能否提供一些示例,说明如何获取搜索的JSON/XML响应? 我目前正在使用以下方法,但可以提高效率 WebRequest request = WebRequest.Create("http://localhost:9090/select?use=MSG&login=lorem&key=08762e43getye0042f875e86eaiu687f&qt=s
WebRequest request = WebRequest.Create("http://localhost:9090/select?use=MSG&login=lorem&key=08762e43getye0042f875e86eaiu687f&qt=search&q="+searchTerm );
string responseFromServer = GetQueryResults(request);
// Parse XML Data
string response = GetResultsFromXML(responseFromServer);
private string GetQueryResults(WebRequest request)
{
//specify protcol
request.ContentType = "application/json";
//request.Headers.
request.Method = "PUT";
////Get Request stream
Stream datastream = request.GetRequestStream();
datastream.Close();
// Get response
WebResponse respose = request.GetResponse();
datastream = respose.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
string responseFromServer = reader.ReadToEnd();
return responseFromServer;
}
private string GetResultsFromXML(string responseFromServer)
{
StringBuilder output = new StringBuilder();
#region XmlDocument
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(responseFromServer);
XmlNodeList nodelist = xdoc.SelectNodes("/response/result/doc");
foreach (XmlNode node in nodelist)
{
DataGridViewRow dgvRow = (DataGridViewRow)dgvResults.Rows[0].Clone();
foreach (XmlAttribute attrib in node.Attributes)
{
if (attrib.Name == "score")
{
dgvRow.Cells[0].Value = attrib.Value;
}
else if (attrib.Name == "docId")
{
dgvRow.Cells[1].Value = attrib.Value;
}
else if (attrib.Name == "pos")
{
dgvRow.Cells[2].Value = attrib.Value;
}
}
XmlNodeList elements = node.SelectNodes("snippet");
foreach (XmlNode node_element in elements)
{
if (node_element.Attributes[0].Value == "title")
{
dgvRow.Cells[3].Value = node_element.InnerText;
}
}
dgvResults.Rows.Add(dgvRow);
}
#endregion
return output.ToString();
}
此外,请求是使用OpenSearchServer API版本1发出的,因为我无法使API2正常工作。任何示例查询都会有所帮助,因为我找不到任何正常工作的exmaples
还尝试执行文档(版本2 API)中给出的示例:
它抛出一个HTTP405-methodnotallowed错误。我希望实现opensearchserver的RESTfulJSONAPI。在现有代码中(使用返回XML的API版本1进行了尝试)
任何想法???您将无法在浏览器中使用此查询,因为它是PUT请求,而不是GET请求 APIv2(Restful)使用不同的HTTP方法,而APIv1只使用GET请求 试着使用例如extension POSTMAN for Chrome来测试不同的查询,它可以轻松地发送包含内容和标题的PUT/POST/GET/DELETE请求 关于C代码,我恐怕无法准确地帮助您 问候,,
Alexandre你能努力改进你的问题吗?更清楚地了解到目前为止您尝试了什么(可能会显示
GetQueryResults
和GetResultsFromXml
,这样我们就可以了解您提高效率的意思了?@Krish,您解决了这个问题吗?我的处境和你一样。谢谢Alexandre。我尝试使用POSTMAN并能够通过PUT请求获得JSON结果。但是在POSTMAN中,我传递参数/查询,如{“query”:“SomeValueToSearch”},URL仍然保持不变。如果我想通过编程的方式通过它怎么办。你能给我举一个URL看起来如何/应该如何生成的例子吗?您好,您将无法仅使用浏览器运行此查询:)浏览器仅发出GET请求。你需要在这里使用PUT。您可以通过编程实现,只需在请求体中传递“{”查询“:“SomeValueToSearch”}”。第一个问题中的代码看起来很有希望,但我不是c#专家。
http://localhost:9090/services/rest/index/my_index/document?login=lorem&key=08762e43getye0042f875e86eaiu687f