【发布时间】:2018-12-25 09:08:29
【问题描述】:
我在其他类似问题中找不到解决方案。 我收到 The remote server returned an error: (400) Bad Request error while running the following code.
string url = @"http://api.dwaybill.com/" + cid +
"/orders.json?v=" + version + "&key=" + key +
"&customer_number=" + customer_number +
"&password=" + password;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes1 = encoding.GetBytes(@"{"Here is the json text that i testet and is correct"}");
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = bytes1.Length;
using (Stream sendStream = request.GetRequestStream())
{
sendStream.Write(bytes1, 0, bytes1.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
}
}
Console.WriteLine(html);
在通过 (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 获得响应时,我收到 400 Bad Request 错误。
我该如何解决这个问题?我该怎么办?
编辑:这是我试图发出帖子请求的 API:https://github.com/digwaybill/Digital-Waybill-API
【问题讨论】:
-
我不熟悉
restapi,但您不需要关闭/刷新请求。您能否单步执行您的代码并发现错误发生的时间? -
如果你使用“using()”你不需要关闭/刷新@PeterSmith
-
@PeterSmith 代码出现在这里 (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 并且状态是协议错误
-
检查
url的内容是否正确。Bad request表示“网关 [is] 无法处理提供的请求 URI,或者 HTTP 请求本身无效”。所以也许url只是不正确/无效/包含空格或类似的东西
标签: c# rest httpwebresponse bad-request http-response-codes