0

I used the top answer from HTTP request with post however an error pops up in visual studio saying 404 remote server not found.

The website exists, it is a rails app bounded to the ip address of my router. Using the following url in the browser updates the attribute of the applicant entity in the rails app. However using the c# app to do this is not working.

http://<ADDRESS HERE>:3000/api/v1/applicants/update?id=2&door=true

I have the ff:

using System.IO;
using System.Net;
using System.Text; 

I used the following code inside a btn click

private void button1_Click (object sender, EventArgs e){
    var request = (HttpWebRequest)WebRequest.Create("http://<ADDRESS HERE>:3000/api/v1/applicants/update");

    var postData = "id=2";
        postData += "&door=true";
    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
Acrux
  • 386
  • 3
  • 26
  • Can you try with Postman. Make sure you are using good request method and check your parameters. Of course, first test if your address is reachable at all :) – IMujagic May 11 '16 at 07:16

1 Answers1

1

Are you sure you need to perform a POST request and not a GET request? I'm asking, because there seems to be inconsistency in your question. First you say you want to get to the url

http://<ADDRESS HERE>:3000/api/v1/applicants/update?id=2&door=true

which is a url with querystring params, but in the code you separate the querystring and send the params as POST data.

The GET request would look something like this

GET http://<ADDRESS HERE>:3000/api/v1/applicants/update?id=2&door=true HTTP/1.1
Host: <ADDRESS HERE>
... (more headers)

And the POST request:

POST http://<ADDRESS HERE>:3000/api/v1/applicants/update HTTP/1.1
Host: <ADDRESS HERE>
Content-type: application/x-www-form-urlencoded
... (more headers)

id=2&door=true
Vedran
  • 757
  • 6
  • 21