【问题标题】:The remote server returned an error: (400) Bad Request?远程服务器返回错误:(400) Bad Request?
【发布时间】:2016-05-23 15:16:21
【问题描述】:

我正在尝试从一个 url 获取响应,它必须是这样的 JSON:

{"request_status": "FAILURE", "error_message": "商户 ID 是 不正确”,“error_code”:“0”,“unique_order_id”:“”}

我在 winForm 的 click 事件处理程序中使用此代码(它有 5 个文本框控件,每个控件处理一个特定参数,我想在文本框 textBoxResponse 中接收 JSON 文件。

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

但是出现异常:

远程服务器返回错误:(400) Bad Request

这个 html 工作正常,我想让我的 winForm 像它一样工作:

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

【问题讨论】:

  • 您调用的 API 似乎无法识别您的请求。检查您是否设置了正确的标头并检查您传递给服务的请求 json。
  • 还注意到您正在使用 WebRequest,使用 HttpWebRequest 是一个更好的选择。在此处查看一些选项以帮助您:stackoverflow.com/questions/9145667/…
  • 请查看我的编辑,我添加的 html 运行良好

标签: c# json http


【解决方案1】:

HTML 表单帖子将发布application/x-www-form-urlencodedmultipart/form-data。您的服务电话正在尝试写application/json。这很可能是您问题的症结所在。

服务是否会接受 JSON(检查它是否接受标头)?如果是这样,您需要在请求标头中进行设置。

或者,将您的代码更改为发布 x-www-form-urlencoded。 See this post for a sample,下面贴了一个 sn-p 供您参考。

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

【讨论】:

    猜你喜欢
    • 2010-12-13
    • 2013-04-20
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多