简体   繁体   中英

HTTP request with post 404 not found

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();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM