如何在.net中模拟post方式提交json数据
最近看见bing的网页上有一个机器人可以根据回答猜人物
http://renlifang.msra.cn/Q20/index.aspx
我想通过代码模拟提交的方式去交互(纯属个人试着玩),我在JS端试了用$.post()的方式去提交数据是可以交互的(必需制定json类型,否则返回错误)。现在我想在.net后台模拟这个动作,不知道该怎么实现。
我自己了用System.Net.HttpWebRequest对象来模拟提交,结果什么也没返回,但是将 request.ContentType = "application/json;charset=UTF-8";改为request.ContentType = "application/x-www-form-urlencoded"可以接受到返回数据,返回的是出错提示。用WebClient我也试过,结果一样。请大神看看是什么问题。
http://renlifang.msra.cn/Q20/index.aspx
我想通过代码模拟提交的方式去交互(纯属个人试着玩),我在JS端试了用$.post()的方式去提交数据是可以交互的(必需制定json类型,否则返回错误)。现在我想在.net后台模拟这个动作,不知道该怎么实现。
$.post(
"/Q20/Handlers/HandlerQ20.ashx",
{ uid: “507937844”, sid: ”809662793“, choice: "yes", a: "GenerateQuestionHandler", questfd: "0"},
function(data){ },
"json"
)我自己了用System.Net.HttpWebRequest对象来模拟提交,结果什么也没返回,但是将 request.ContentType = "application/json;charset=UTF-8";改为request.ContentType = "application/x-www-form-urlencoded"可以接受到返回数据,返回的是出错提示。用WebClient我也试过,结果一样。请大神看看是什么问题。
string url = "http://renlifang.msra.cn/Q20/Handlers/HandlerQ20.ashx";
string param = "{\"uid\":\"246534824\",\"sid\":\"906508525\",\"choice\":\"yes\",\"a\":\"GenerateQuestionHandler\",\"questfd\":\"0\"}";
string callback = PostMoths(url,param);
public static string PostMoths(string url, string param)
{
string strURL = url;
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string paraUrlCoded = param;
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
return strValue;
}
...全文
相关推荐
一步一步学习ASP.NET MVC1.0.pdf
一步一步学习ASP.NET MVC1.0.pdf 一步一步学习ASP.NET MVC1.0.pdf
C#通过post提交json字符串
C#通过post提交json字符串示例
.net 后台Post,get方式调用webapi
.net 后台Post,get方式调用webapi,包括上传文件
C# post方式提交Form表单
C# post方式提交Form表单,可以支持带参数传入,供大家学习参考。
HTTP Post提交Json与接收返回结果 Winform程序
HTTP Post提交与接收Json Winform程序,实现Json的提交和接收


