1

I'm trying to get the response from a url, which must be JSON like this:

{"request_status": "FAILURE", "error_message": "Merchant ID is incorrect", "error_code": "0", "unique_order_id": ""}

I use this code in the click event handler in my winForm (it has 5 textbox controls each handle a specific parameter, I want to recieve the JSON file in the textbox textBoxResponse.

private void button1_Click(object sender, EventArgs e)
{
    WebRequest request = WebRequest.Create("http://test5.paymobsolutions.com/api/merchant/pay_order_online/");
    request.Method = WebRequestMethods.Http.Post;
    //request.Headers.Add("REF-1", "me1");
    //request.Headers.Add("REF-2", "me2");
    request.Proxy = null;
    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
        PaymentParamters paras = new PaymentParamters { c_cvv_U = textBoxCVV.Text, c_expiry_mm_U = textBoxExM.Text, c_expiry_yy_U = textBoxExY.Text, c_holder_name_U = textBoxHolderName.Text, c_pan_U = textBoxPAN.Text };

        string json = JsonConvert.SerializeObject(paras);
        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();
    }
    var response = request.GetResponse();
    using (var streamReader = new StreamReader(response.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
        textBoxResponse.Text = result;
    }
}

But an exception occurs:

The remote server returned an error: (400) Bad Request

This html works fine, I want to my winForm to act like it:

<form method="post" action="http://test5.paymobsolutions.com/api/merchant/pay_order_online/">
    <input type="text" placeholder="pan"            name="c_pan_U" value="" />
    <input type="text" placeholder="holder_name"    name="c_holder_name_U" value ="" />
    <input type="text" placeholder="expiry_mm"      name="c_expiry_mm_U" value="" />
    <input type="text" placeholder="expiry_yy"      name="c_expiry_yy_U" value="" />
    <input type="text" placeholder="cvv"            name="c_cvv_U" value="" />
    <input type="submit" />
</form>
mshwf
  • 5,595
  • 11
  • 42
  • 100
  • 1
    It seems like the API you are calling is not recognizing your request. Check that you have the correct headers set and check the request json you are passing to the service. – Murray Foxcroft May 23 '16 at 15:22
  • 1
    Also noticed you are using WebRequest, using HttpWebRequest is a better option. See some options here to help you along: http://stackoverflow.com/questions/9145667/how-to-post-json-to-the-server – Murray Foxcroft May 23 '16 at 15:23
  • Please view my edit, I've added html working good – mshwf May 23 '16 at 15:43

1 Answers1

0

The HTML form post will be POSTing either application/x-www-form-urlencoded or multipart/form-data. Your service call is trying to write application/json. This could well be the crux of your problem.

Will the service accept JSON (check it's accept headers)? If so, you need to set this in your request header.

Alternatively, change your code to post x-www-form-urlencoded. See this post for a sample, a snippet has been posted below for your reference.

var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");

var postData = "thing1=hello";
    postData += "&thing2=world";
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Community
  • 1
  • 1
Murray Foxcroft
  • 10,865
  • 2
  • 52
  • 71