【问题标题】:HTTP request with post 404 not found未找到 404 后的 HTTP 请求
【发布时间】:2016-05-11 15:46:27
【问题描述】:

我使用了HTTP request with post的最佳答案 但是,Visual Studio 中会弹出一个错误,提示找不到 404 远程服务器。

该网站存在,它是一个绑定到我的路由器IP地址的rails应用程序。 在浏览器中使用以下 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();
}

【问题讨论】:

  • 你可以试试 Postman。确保您使用了良好的请求方法并检查您的参数。当然,首先测试您的地址是否可以访问:)

标签: c# visual-studio httprequest


【解决方案1】:

您确定需要执行POST 请求而不是GET 请求吗?我在问,因为您的问题似乎不一致。首先你说你想访问 url

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

这是一个带有查询字符串参数的 url,但在代码中,您将查询字符串分开并将参数作为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

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 2021-05-08
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    相关资源
    最近更新 更多