所以基本上我试图向http://op.gg/mmr/ajax/mmr.json发送一个 json 请求,json 会得出 {"summonerName":"DeCodux"},但我的问题是它看起来像这样代码不会将任何内容发送到其网络服务器上的 mmr.json 中。
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://op.gg/mmr/ajax/mmr.json");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
summonerName = "DeCodux"
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
DataBox.Text = result;
}
}
这段代码也不会;
var request = (HttpWebRequest)WebRequest.Create("http://op.gg/mmr/ajax/mmr.json/");
request.Method = "POST";
request.Host = "op.gg";
request.Accept = "application/json, text/javascript, */*";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.Referer = "http://op.gg/mmr/region=EUW";
//request.ContentLength = 27;
//request.Connection = "keep-alive";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
string writeit = "{\"summonerName\":\"DeCodux\"}";
// string writethis = "{\"region\"=\"EUW\"," + "\"userName\"=\"DeCodux\"}";
writer.Write(writeit);
}
var response = request.GetResponse();
var stream = response.GetResponseStream();
var json = "";
if (stream == null) return;
using (var reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
json += reader.ReadLine();
}
}
#endregion oldtest
/* var serializer = new JavaScriptSerializer();
var x = (Dictionary<string, object>)serializer.DeserializeObject(json);
MyData data = serializer.Deserialize<MyData>(x.ToString());*/
/* var jss = new JavaScriptSerializer();
var data = jss.Deserialize<Dictionary<string, string>>(json);
*/
DataBox.Text = json;
我只是注释掉了序列化和字典部分,因为我可以轻松处理这些部分。此外,当我现在从服务器检索原始响应时,我得到:
{"error":true,"errormsg":"\uc18c\ud658\uc0ac\uba85\uc744 \uc785\ub825\ud574\uc8fc\uc138\uc694."}
解析和翻译的是
{
"error": true,
"errormsg": "Please enter the name of the Summoner." }
因此,由于它给了我该错误作为响应,因此我的 json 请求似乎没有发送到他们的服务器。如果请求将被发送但它不正确,它会给我其他错误,例如“此召唤者没有 ---”等等。我还使用 firebug “net” 模式浏览了所有发布请求标头等。
所以我的问题是,这里出了什么问题?为什么它不发送 POST 请求?