我正在尝试在我的 C# ASP.Net MVC3 应用程序中调用 Web 服务。这是源代码:
public string getCourseSchedule()
{
string url = "http://192.168.1.198:15014/ShoppingCart2/CourseSchedule";
string data = "Months&StatesMX&Zip=&Miles=&ProgramCodes=&EventCode=&PaginationStart=1&PaginationLimit=3";
byte[] bytes = Encoding.UTF8.GetBytes(data);
var myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.Method = "POST";
myReq.ContentLength = data.Length;
myReq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
string responseString = "";
using (Stream requestStream = myReq.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
using (HttpWebResponse response = (HttpWebResponse)myReq.GetResponse())
{
HttpStatusCode statusCode = response.StatusCode;
if (statusCode == HttpStatusCode.OK)
{
responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
}
return responseString;
}
代码返回“400 Bad Request”错误。这就是我在 javascript 中的操作方式,并且可以正常工作。
Mexico_Schedule: {"Months": null,
"States": [{"State: "MX"}],
"Zip": "",
"Miles": "",
"ProgramCodes": null,
"EventCode": null
"PaginationStart": 1,
"PaginationLimit": 3
};
$.ajax({
async: true,
cache: false,
type: 'POST',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
url: "http://192.168.1.198:15014/ShoppingCart2/CourseSchedule",
data: JSON.stringify(Mexico_Schedule),
dataType: 'json',
success: function (data) {
console.log('Fired when the request is successful');
// Do something with results
}
});
我需要进行哪些修改才能使 C# 版本正常工作?