POST与WebRequest问题

时间:2011-09-14 13:45:13

标签: c# .net asp.net post

这是Google Chrome服务的POST消息: This is the POST message to service of Google Chrome: 我想从C#发布此POST消息:

System.Net.WebRequest req = 
System.Net.WebRequest.Create(@"http://www.ччч.ru/SystemService.asmx/VotingFoPhoto");
req.Method = "POST";
req.Timeout = 12000;
req.ContentType = "application/json; charset=UTF-8";

byte[] sentData = Encoding.GetEncoding(1251).GetBytes("photoId=E6A0327A&concursId=3");
req.ContentLength = sentData.Length;
System.IO.Stream sendStream = req.GetRequestStream();
sendStream.Write(sentData, 0, sentData.Length);
sendStream.Close();

req.GetResponse();

当我运行此代码时,我得到500服务器错误。请帮帮我。

1 个答案:

答案 0 :(得分:3)

您已告知服务器content-typeapplication/json。但是您提供了application/x-www-urlencoded格式的内容。

尝试提供JSON格式:

byte[] sentData = 
  Encoding.GetEncoding(1251).GetBytes("{'photoId':'E6A0327A';'concursId':3}"); 

或者尝试更改内容类型标题。