1

I'm a bit clueless on this so please excuse my lack of code examples.

Basically I've got a requirement to pass in a json 'string' array to a php page which in turn should give me back a response.

so firstly I need to create a string and serialize (?) this into a json object and pass it to a specified url....so firstly I'm not sure that I'm going about this in the correct way and then pass this to a URL:

protected void cmdConnect_Click(object sender, EventArgs e)
{
    var sig = FormsAuthentication.HashPasswordForStoringInConfigFile(DateTime.Now.ToString("yyyy-MM-dd") + "!Cv*z]&z7:c,+zW", "SHA1");
    var model = "Array (shortened for ease))";

    var json = JsonConvert.SerializeObject(model);
    string data = HttpUtility.UrlEncode(json);
    byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(data);

    curlPost(data);
    //jsonString.Text = json;


}

protected void curlPost(string data)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dev.com/shopping_cart.php");
    request.Method = "POST";
    request.ContentType = "application/json";
    byte[] buffer = UTF8Encoding.UTF8.GetBytes(data);
    request.ContentLength = buffer.Length;
    using (Stream oStream = request.GetRequestStream())
    {
        oStream.Write(buffer, 0, buffer.Length);
    }
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        string myResponse = reader.ReadToEnd();
        jsonString.Text = myResponse;

    }
}

But although I'm getting a response (myResponse) the data that I'm passing in has no impact on it so I'm thinking that I'm either not constructing it properly or I'm not passing it in at all. What I should get back is a 'key' but what I'm actually getting back is the HTML for the php page.

I know that this is really vague but a) this is all brand new to me and b) as such I'm not sure that of the terminology involved.

Any pointers would be greatly appreciated.

Thanks, Craig

Bhanu Chandra
  • 408
  • 8
  • 26
SxChoc
  • 619
  • 3
  • 10
  • 26
  • The HTML of the PHP Page ? What do you mean ? YOu see the PHP code ? Do you have an apache server (or equivalent) ? What do you see when you do a GET request here? `http://dev.com/shopping_cart.php` – Kevin Nov 23 '15 at 17:02
  • Yup, I'm getting the HTML of the rendered PHP page ie. the variable myResponse = " – SxChoc Nov 23 '15 at 17:07
  • If you don't want a doctype etc. returned then don't output it in the php. Just output the one thing you want. – developerwjk Nov 23 '15 at 17:14
  • Not sure that I understand, the HTML is what's being returned. It's not what I want (I'm after a key that the PHP page should generate based upon the json that I'm passing to it) but that's not's what's being returned so obviously I've screwed up somewhere, thanks, – SxChoc Nov 23 '15 at 17:16
  • you have to ensure that it's definitely valid json that you're sending. assuming it is, then possibly your code is incorrect for sending the POST. in particular make sure that you add `request.Accept = "application/json";` -> have a quick skim on this SO question: http://stackoverflow.com/questions/9145667/how-to-post-json-to-the-server – jim tollan Nov 23 '15 at 17:18
  • Cheers buddy (there's a great chance that all parts of my code are wrong!) – SxChoc Nov 23 '15 at 17:22

0 Answers0