简体   繁体   English

我如何在Windows窗体应用程序上使用$ .getJSON

[英]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.

相关问题 如何使用JQuery getJSON验证用户名 - How can I use JQuery getJSON to verify usernames 如何在 macOS 上使用 windows 表单应用程序在这种情况下我可以使用哪个版本的 vs 代码? - How can I use the windows form application on macOS which version o f vs code could i use in this situation? 如何在Windows应用程序中增加表单加载上的文本框内容 - How can I increment a textbox content on form load in windows application 如何在Windows窗体应用程序中获取当前窗体? - How can I get the current form in a Windows Forms application? 如何从Windows Mobile应用程序实现向导? - How can I implement a Wizard form a Windows Mobile application? 我如何检查C#Windows应用程序中的窗体是否打开? - How can i check form is open in c# windows application? 如何在Windows Form和XML中使用DataRepeater控件? - How can i use DataRepeater Control in Windows Form with XML? 如何在Windows窗体中使用Google文本语音API? - How can I use google text to speech api in windows form? 如何从 C# 中的其他窗体 Windows 应用程序控制应用程序? - How can I control the a application from a other form windows application in C#? 如何制作绘图表面,以便人们可以在Windows Form应用程序上绘图? - How can I make a drawing surface so people can draw on my Windows Form application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM