【问题标题】:HttpWebRequest-The remote server returned an error: (400) Bad RequestHttpWebRequest-远程服务器返回错误:(400) Bad Request
【发布时间】:2012-12-25 02:02:55
【问题描述】:

运行以下代码时,远程服务器返回错误:(400) Bad Request 错误。 我正在尝试在 http 服务器上上传 xml 文件。 我的 xml 文件包含用户名、密码和域的标记,当我尝试手动连接时,我能够连接它,但是当我尝试通过此代码连接它时使用相同的凭据,我收到 400 Bad Request 错误. 请建议我如何克服这个问题。 谢谢 `

  public static void UploadHttp(string xml)     
    {

        string txtResults = string.Empty;
        try
        {
            string url = "http://my.server.com/upload.aspx ";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.KeepAlive = false;
            request.SendChunked = true;
            request.AllowAutoRedirect = true;
            request.Method = "Post";
            request.ContentType = "text/xml";
            var encoder = new UTF8Encoding();
            var data = encoder.GetBytes(xml);
            request.ContentLength = data.Length;
            var reqStream = request.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
            WebResponse response = null;
            response = request.GetResponse();
            var reader = new StreamReader(response.GetResponseStream());
            var str = reader.ReadToEnd();
        }
        catch (WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError)
            {
                HttpWebResponse err = ex.Response as HttpWebResponse;
                if (err != null)
                {
                    string htmlResponse = new StreamReader(err.GetResponseStream()).ReadToEnd();
                    txtResults = string.Format("{0} {1}", err.StatusDescription, htmlResponse);
                }
            }
            else
            {

            }
        }
        catch (Exception ex)
        {
            txtResults = ex.ToString();
        }
    }`

【问题讨论】:

  • 我怀疑它会有所不同,但您可以尝试从 URL 中删除尾随空格并使用“POST”块大写(或者必须有一个常量?)。您可以在服务器日志中看到任何内容吗?您还可以尝试在 Fiddler 中手动或通过应用程序捕获这两个请求以查找差异。不过,您的 ASPX 是否肯定接受 XML 文本的 POST?
  • 不,它没有任何区别!

标签: c# .net upload httpwebrequest


【解决方案1】:

//使用“ASCII”或尝试使用其他编码方案而不是“UTF8”。

using (StreamWriter postStream = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.UTF8))
{
    postStream.Write(postData);
    postStream.Close();
}

【讨论】:

    【解决方案2】:

    400 Bad request 由于不正确的身份验证条目将引发错误。

    1. 检查您的 API URL 是否正确。不要附加或前置空格。
    2. 验证您的用户名和密码是否有效。请在输入时检查任何拼写错误。

    注意:大部分是由于不正确的身份验证条目由于拼写更改会发生 400 Bad request。

    【讨论】:

    • 对我来说,它是 API URL。我忘记在我的 PUT 请求中输入我想要更新的模型的 ID。谢谢!
    【解决方案3】:

    您使用什么类型的身份验证? 使用 Ben 之前所说的属性发送凭据并设置 cookie 处理程序。 您已经允许重定向,如果发生任何重定向,请检查您的网络服务器(NTLM 身份验证确实如此)。如果有重定向,您需要存储会话,该会话主要存储在会话 cookie 中。

    【讨论】:

      【解决方案4】:

      您确定应该使用 POST 而不是 PUT?

      POST 通常与application/x-www-urlencoded 格式一起使用。如果您使用的是 REST API,您可能应该使用 PUT?如果您要上传文件,您可能需要使用multipart/form-data。并非总是如此,但通常这是正确的做法..

      此外,您似乎没有使用凭据登录 - 您需要使用 HttpWebRequest 对象的 Credentials 属性来发送用户名和密码。

      【讨论】:

      • REST api中的POST用于创建,PUT用于更新数据。所以两者都可以,取决于他想做什么。
      猜你喜欢
      • 2010-12-13
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多