我对网络编程比较陌生。我有一个 Windows 窗体试图通过 ajax 和 java 脚本与 Web 服务进行通信。我的表格有以下代码,
string sURL = <<URL>>;
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(sURL);
//Modify request properties
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = false;
request.AllowWriteStreamBuffering = false;
string sRequestData = JsonConvert.SerializeObject(new GetSessionInfo() { UserName = "xxxxxx", Database = "xxxxxx", Module = "xxxxxx" }.ToString());
StreamWriter sWriter = new StreamWriter(request.GetRequestStream());
sWriter.Write(sRequestData);
sWriter.Close();
try
{
//Extract the response
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response == null)
return false;
StreamReader responcestream = new StreamReader(response.GetResponseStream());
string ResponseData = responcestream.ReadToEnd();
responcestream.Close();
return true;
}
catch
{
throw;
}
我的 index.html 文件如下所示
function Session()
{
$.ajax({
type: "POST",
url: "/ProcessRequestFromEasyOrder",
data : JSON.stringify({ user:"****", Database:"****", module:"****"}),
contentType: "application/json; charset=utf-8",
dataType: "application/json",
async: true,
cache: false,
cors: true,
success:function(data){
alert(data);
}
});
}
目前,我可以通过浏览器硬编码参数来调用 Web 服务方法。但是,当我尝试通过我的 Windows 窗体执行此操作时,它总是返回我 (405)Method Not Allowed。有人可以帮我回答以下问题吗,
- 这可以实现吗?如果是这样,你能提供一个实现这一目标的例子吗?
- 是因为网络服务设置的问题吗?如果是这样,我该如何解决?
- 有什么替代方法可以做到这一点?一个例子会很棒
我在 .NET 4.5 上非常感谢您的帮助!