HttpWebResponse.GetResponse错误400,但是post与postman一起使用

时间:2019-06-26 15:36:43

标签: c# postman httpwebresponse http-status-code-400

我在处理某些代码时遇到麻烦,它在几天前就已经运行了,但是现在不确定为什么返回错误400。

当我接受请求并将其粘贴到邮递员中时,它可以工作。我也尝试过尝试更改该方法,尽管它不正确,但仍然会失败

string strResponseValue = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);

request.Method = httpMethod.ToString();

request.Headers.Add("Authorization", "***PRIVATEKEYREMOVED***");

if (((request.Method == "POST")||(request.Method == "PUT")) && postJSON != string.Empty)
{
    request.ContentType = "application/json";
    using (StreamWriter swJSONPayload = new StreamWriter(request.GetRequestStream()))
    {
        swJSONPayload.Write(postJSON);
        swJSONPayload.Close();
    }
}
try
{
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        {
            //throw new ApplicationException("some error");//for some reason we got here, display why...
        }
        using (Stream responseStream = response.GetResponseStream())
        {
            if (responseStream != null)
            {
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    strResponseValue = reader.ReadToEnd();
                }
            }
        }
    }
}
catch (WebException e)
{

}

它应该返回一个带有记录的json响应字符串,然后我将该记录发送到数据集并进行操作,但它甚至没有到达那里

0 个答案:

没有答案