2016-05-11 98 views
0

我用HTTP request with post 的最佳答案但是在Visual Studio中彈出一個錯誤,說404找不到遠程服務器。HTTP請求404後沒有找到

該網站存在,它是一個軌道應用程序綁定到我的路由器的IP地址。 在瀏覽器中使用以下URL更新rails應用程序中申請實體的屬性。但是,使用C#應用程序來做到這一點是行不通的。

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

我有FF:

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

我用了一個BTN點擊

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

你可以嘗試用郵差。確保你使用的是很好的請求方法並檢查你的參數。當然,首先測試你的地址是否可達:) – hellwd

回答

1

內部下面的代碼是否確實需要執行POST請求,而不是GET請求?我在問,因爲你的問題似乎有不一致之處。首先,你說你想要得到的url

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

這與查詢字符串PARAMS一個網址,但在你的代碼中分離查詢字符串併發送PARAMS爲POST數據。

GET請求將是這個樣子

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

而且POST請求:

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 
+0

使用get works! – Acrux

相關問題