[英]How can i use $.getJSON on windows form application
Code below is working but i want to use this by httpwebrequest in my windows form application to get data. 下面的代码正在工作,但我想在Windows窗体应用程序中通过httpwebrequest使用它来获取数据。
$.getJSON("JSONHandler", {
metod: "mymethod",
param: {
id: "123",
name: "Jhon",
surname: "Tiger",
birthdate: "7.2.1949"
}
}, function (json) {
if (json.Mesaj)
return alert(json.Mesaj);
if (json.hasinfo) {
PageMethods.MyMethod(JSON.stringify(json.phoneNo));
}
else {
alert("Something wrong");
}
});
I try to code below 我尝试在下面编码
var httpWebRequest = (HttpWebRequest)WebRequest.Create("JSONHandler");
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{" +
" \"metod\": \"mymethod\", " +
" \"param\": { " +
" \"id\": \"123\", " +
" \"name\": \"Jhon\", " +
" \"surname\": \"Tiger\", " +
" \"birth\": \"7.2.1949\" " +
" } " +
" }";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
//Now you have your response.
//or false depending on information in the response
}
But it returns nullpointer exception error from server i thing my params are wrong. 但是它从服务器返回nullpointer异常错误,我的参数错误。 What must i do?
我必须做什么?
I think you can do this from you windows forms application by using httpClient 我认为您可以使用httpClient从Windows窗体应用程序执行此操作
Here is example code, that sends a bunch of products to a REST service. 这是示例代码,它将大量产品发送到REST服务。
Update: This code should work with the data from you example. 更新:此代码应与您示例中的数据一起使用。
var person = new {
id= "123",
name= "Jhon",
surname= "Tiger",
birthdate= "7.2.1949"
}
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("JSONHandler");
var result = client.PostAsJsonAsync("/mymethod", person).Result;
switch (result.StatusCode)
{
case HttpStatusCode.OK:
Trace.TraceInformation("Updated ok");
break;
default:
Trace.TraceError("Something went wrong");
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.