如何在.net中模拟post方式提交json数据

hurricanezhu0321 hurricanezhu0321 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近看见bing的网页上有一个机器人可以根据回答猜人物
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;
}


...全文
评论 10
收藏 已收藏
分享
返回顶部
hurricanezhu0321
C# 111,129 成员 642,538 内容