0

I'm sending a JSON post using C#. Everything is working fine if I hardcode the values directly in the request. But I want to send that in a form of variable, but is failing. I tried different ways and I couldn't find any solution. I'm trying to get the value from 'num' variable which is 172024 in the ID field, but in the response I'm getting the string as is, not the value.

Here is my code

static void Main(string[] args)
{
    //Make a Json request

    var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://IPaddress/apibxe_json.php");

    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";

    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string num;
        num = Convert.ToString("172024");
        Console.WriteLine(num);

        string json = "[ { \"connection\" : { \"PS\": \"99778\", \"pr\" : \"******\" }},  {\"execute\" : { \"name\" : \"NewAPI\", \"params\" : { \"Action\" : \"NEW\", \"ID\":  \"$num\" ,   \"Dlr\" : \"&&&&&\"}}}]";

        streamWriter.Write(json);
    }

    //Get the response
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();

        JArray jresponse = JArray.Parse(responseText);

        Console.WriteLine(jresponse);  
    }
}
Craig W.
  • 17,838
  • 6
  • 49
  • 82
user3381098
  • 93
  • 1
  • 5
  • 13

2 Answers2

1

As an alternative to string concatenation, you might want to consider creating a class to represent the JSON you're writing into the request body and serialize that instead so that it's a bit easier to work with.

I noticed that you're already using JSON.NET-- here's how you would do that with that library (classes generated using json2csharp. They could use some cleaning up, but this is just an example):

public class Connection
{
    public string PS { get; set; }
    public string pr { get; set; }
}

public class Params
{
    public string Action { get; set; }
    public int ID { get; set; }
    public string Dlr { get; set; }
}

public class Execute
{
    public Execute()
    {
        this.Parameters = new Params();
    }

    public string name { get; set; }

    [JsonProperty("params")]
    public Params Parameters { get; set; }
}

public class Request
{
    public Request()
    {
        this.connection = new Connection();
        this.execute = new Execute();
    }

    public Connection connection { get; set; }
    public Execute execute { get; set; }
}

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    var request = new Request();

    /* Set other properties as well */
    request.execute.Parameters.ID = 172024;

    string json = JsonConvert.SerializeObject(request);

    streamWriter.Write(json);
}
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
0

The code snip could be helpful for you(on MVC)....

 public JsonResult LoadName(string temp)
 {
      var fromBd=temp+" Bangladesh";
      return Json(fromBd,JsonRequestBehavior.AllowGet);
 }

Jquery function is ....

function(){
var temp='From';
$.get("/BasicSettings/Ajax/LoadName", { temp: temp}, function (data) {
            $('span').html(data.fromBd); 
        });
}
Rejwanul Reja
  • 1,339
  • 1
  • 17
  • 19
  • I changed the code and is working. Thanks for help, really appreciated. I'm new to C# and was trying to learn more one step at a time, Please suggest some beginner-intemediate books if there are any. – user3381098 Feb 05 '15 at 20:14