将JSON curl转换为C#
我有以下curl代码:将JSON curl转换为C#,c#,curl,solr,httpwebrequest,C#,Curl,Solr,Httpwebrequest,我有以下curl代码: curl 'localhost:8983/solr/sessions/update?commit=true' -H 'Content-type:application/json' -d '[{"Session_SessionId":"da7007e9-fe7a-4bdf-b9e4-1a55034cf08f","Session_HasComments":{"set":true}}]' 我正在尝试转换为C#,但每次都会出现错误,因此无法确定我的代码是否正确 以下是我目前掌握
curl 'localhost:8983/solr/sessions/update?commit=true' -H 'Content-type:application/json' -d '[{"Session_SessionId":"da7007e9-fe7a-4bdf-b9e4-1a55034cf08f","Session_HasComments":{"set":true}}]'
我正在尝试转换为C#,但每次都会出现错误,因此无法确定我的代码是否正确
以下是我目前掌握的情况:
string path = "http://localhost:8983/solr/sessions/update?commit=true";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(path);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"Session_SessionId\":\"" + sessionId + "\"," +
"\"" + fieldName + "\":{\"set\":\"" + fieldValue + "\"}}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
这一行似乎总是出现错误()
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
我得到的错误是:
The remote server returned an error: (400) Bad Request.","StackTrace":" at System.Net.HttpWebRequest.GetResponse()
有什么想法吗?提前谢谢
Dave也许是因为您删除了JSON内容中的方括号,才将其流式传输到请求中?尝试将[]添加回数据的开始/结束。虽然“BadRequest”通常是一个非常严格的错误,它告诉您HTTP请求的格式不正确,但您的服务器实际上也可能会在其他情况下返回该代码,如丢失会话id,这可能发生在此处 请注意差异:
-d '[{"Session_SessionId":"da70.....
^ bracket
及
在数据末尾也一样。当然,这只是一个猜测 服务器无法理解您的请求。您是否检查了json变量的输出。我认为JSON字符串没有正确生成
为什么不使用创建JSON字符串。将更新处理程序与JSON对象一起使用需要遵循中概述的JSON格式,因为另一个用户建议您的JSON输出与CURL不匹配。尝试以下操作,而不是键入文本
var data = new[]
{
new
{
Session_SessionId = "da7007e9-fe7a-4bdf-b9e4-1a55034cf08f",
Session_HasComments = new {set = true}
}
};
var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(data);
此外,您正在使用using
关键字来写入数据,并尝试在块内处理响应-这可能会有所不同,但将其移到块外可能是值得的
最后,您可能需要将数据编码为字节数组
下面是实现上述建议的代码
string path = "http://localhost:8983/solr/sessions/update?commit=true";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(path);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
var data = new[]
{
new
{
Session_SessionId = "da7007e9-fe7a-4bdf-b9e4-1a55034cf08f",
Session_HasComments = new {set = true}
}
};
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(data);
byte[] byteData = new System.Text.ASCIIEncoding().GetBytes(json);
httpWebRequest.ContentLength = byteData.Length;
using (Stream stream = httpWebRequest.GetRequestStream())
{
stream.Write(byteData,0,byteData.Length);
}
HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
string respStr = new StreamReader(resp.GetResponseStream()).ReadToEnd();
Console.WriteLine("Response : " + respStr);
就用吧。如果您使用Solr 4+,您需要下载最新的代码并自己构建,但这非常简单。Dave,它对我很有用 您缺少方括号。只需将相应的行替换为以下内容: 字符串json=“[{\”会话\会话ID\”:\“”+SessionId+“\”,”+
“\”“+fieldName+”\:{\“set\”:\”“+fieldValue+“\”}}]” 你犯了什么错误?localhost:8983是否处于活动状态?您应该提供您得到的错误。我得到的错误是:远程服务器返回了一个错误:(400)错误请求。“,”System.Net.HttpWebRequest.GetResponse()上的StackTrace是的localhost肯定处于活动状态我相信我已经注意到了这一点,原来的curl可以工作,我只需要同样的C语言实现,加上方括号,再也没有错误了!谢谢你,现在来看看为什么它没有更新。。。
string path = "http://localhost:8983/solr/sessions/update?commit=true";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(path);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
var data = new[]
{
new
{
Session_SessionId = "da7007e9-fe7a-4bdf-b9e4-1a55034cf08f",
Session_HasComments = new {set = true}
}
};
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(data);
byte[] byteData = new System.Text.ASCIIEncoding().GetBytes(json);
httpWebRequest.ContentLength = byteData.Length;
using (Stream stream = httpWebRequest.GetRequestStream())
{
stream.Write(byteData,0,byteData.Length);
}
HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
string respStr = new StreamReader(resp.GetResponseStream()).ReadToEnd();
Console.WriteLine("Response : " + respStr);