0

Code below is working but i want to use this by httpwebrequest in my windows form application to get data.

$.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. What must i do?

crush
  • 16,713
  • 9
  • 59
  • 100
  • Is there a way to use this function on server side? – Android Soft Test Sep 18 '15 at 14:25
  • Show us the server side code that is handling this, or a definition of its contract. Also, why are you doing a `POST` in `C#` and not a `GET` like you do with the `$.getJSON` request? – crush Sep 18 '15 at 14:26
  • Actually JSONHandler in the code is an url (for security reason i just write JSDONHandler) and i can not access server side code. I just get data from server with above first javascript code. – Android Soft Test Sep 18 '15 at 14:31
  • Well you must be violating the contract set by JSONHandler. So, without knowing the contract that JSONHandler expects, we can't know what you are doing wrong information wise. You appear to be sending valid JSON. It just seems that it's not what is expected by the server. – crush Sep 18 '15 at 15:01
  • We have a working code on javascript and it send variables with Jquery and variables are metod and param. And working example send variables with { metod: "mymethod", param: { id: "123", name: "Jhon", surname: "Tiger", birthdate: "7.2.1949"} – Android Soft Test Sep 18 '15 at 15:17
  • You probably need send as a`GET` and not a `POST`. That means your data should be in the form of url parameters, not a payload. – crush Sep 18 '15 at 15:27
  • I think you are right i need to use GET because i try the url with http://JSonHandler/?metod=mymethod is working but i can not add the parameters – Android Soft Test Sep 18 '15 at 15:46
  • Use Chrome Developer Tools or Fiddler to capture the raw GET request leaving your browser when you execute `$.getJSON`. It will show you the format of the query parameters. You'd then add those query parameters to the URI you pass to `WebRequest.Create`. No need for the `StreamWriter`. – crush Sep 18 '15 at 16:37
  • http://stackoverflow.com/a/1877016/1195273 Is a good answer demonstrating how you can build the query string programmatically. The value of your query string parameter `param` would be JSON. – crush Sep 18 '15 at 16:39

1 Answers1

0

I think you can do this from you windows forms application by using httpClient

Here is example code, that sends a bunch of products to a REST service.

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;
    }
}
devzero
  • 2,510
  • 4
  • 35
  • 55
  • HttpClient is just another mechanism for sending raw HTTP requests like HttpWebRequest. He doesn't need to switch mechanisms to solve his problem, even though HttpClient is preferred to HttpWebRequest. – crush Sep 18 '15 at 15:00
  • Thank you for your answer devzero but it is not my solution may be another method. The question is if Jquery Jsonget method send variables code above what is the way to send variables for HttpWebRequest or HttpClient . – Android Soft Test Sep 18 '15 at 15:10
  • by the way when i try httpwebrequest it returns {"Mesaj":"java.lang.NullPointerException","Exception":true} – Android Soft Test Sep 18 '15 at 15:13
  • First parameter of PostAsJsonAsync method is the "method" part, second parameter is the paramters. You just build a C# object that you send in, you can even you an anonymous object. – devzero Sep 19 '15 at 16:22