post请求,提示“远程服务器返回错误: (400) 错误的请求”这是什么原因

yunfan
2019-03-30 10:18 878 0

C#代码:

```
HttpWebRequest webrequest = null;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
webrequest = WebRequest.Create(url) as HttpWebRequest;
webrequest.ProtocolVersion = HttpVersion.Version10;
}
else
{
webrequest = WebRequest.Create(url) as HttpWebRequest;
}
//HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
webrequest.Method = "post";
webrequest.ContentType = "application/json;charset=utf-8";
byte[] postdatabyte = Encoding.UTF8.GetBytes(postData);
webrequest.ContentLength = postdatabyte.Length;
Stream stream;
stream = webrequest.GetRequestStream();
stream.Write(postdatabyte, 0, postdatabyte.Length);
stream.Close();
using (var httpWebResponse = webrequest.GetResponse())
using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
{
string ret = responseStream.ReadToEnd();
string result = ret.ToString();
return result;
}

1个回答

热门排序