【问题标题】:400 Bad Request in CurlCurl 中的 400 错误请求
【发布时间】:2012-10-13 14:36:06
【问题描述】:

几天来,我一直在尝试发布到服务器。但我收到一个错误的请求错误:

HTTP/1.1 400 Bad Request
Server: cloudflare-nginx
Date: Sat, 13 Oct 2012 14:25:32 GMT
Content-Type: text/html
Content-Length: 522
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare-nginx</center>
</body>
</html>
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a padding to disable MSIE and Chrome friendly error page -->
<!-- a pad 

我的代码在这里(我在 c# 上使用了 libcurl.net

Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
Thread.Sleep(400);
try
{
    var curl = new Easy();

    Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);

    curl.SetOpt(CURLoption.CURLOPT_URL, webpage);
    curl.SetOpt(CURLoption.CURLOPT_HEADER, true);
    curl.SetOpt(CURLoption.CURLOPT_TIMEOUT, "100");
    curl.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, false);

    Slist sl = new Slist();

    sl.Append(String.Format("POST {0} HTTP/1.1\r\n\r\n", (new Uri(oyy.sayfa).PathAndQuery)));
    sl.Append(String.Format("Host: {0}", hostname ));
    sl.Append(String.Format("{0}", "Connection: keep-alive"));
    sl.Append(String.Format("Content-Length: {0}", param.Length));
    sl.Append(String.Format("Origin: {0}", originname));
    sl.Append(String.Format("{0}", "X-Requested-With: XMLHttpRequest"));
    sl.Append(String.Format("User-Agent: {0}",  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4")); 

    sl.Append(String.Format("{0}", "Content-Type: application/x-www-form-urlencoded"));
    sl.Append(String.Format("{0}", "Accept: */*"));
    sl.Append(String.Format("Referer: {0}",referans)); 
    sl.Append(String.Format("{0}", "Accept-Encoding: gzip,deflate,sdch"));
    sl.Append(String.Format("{0}", "Accept-Language: en-US,en;q=0.8"));
    sl.Append(String.Format("{0}", "Accept-Charset: utf-8;q=0.7,*;q=0.3*/"));
    sl.Append("\r\n\r\n");

    curl.SetOpt(CURLoption.CURLOPT_HTTPHEADER, sl); 

    curl.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile);
    curl.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile);
    curl.SetOpt(CURLoption.CURLOPT_COOKIE, "cokieee");
     curl.SetOpt(CURLoption.CURLOPT_PROXY, "http://127.0.0.1:9050");
    curl.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_SOCKS5);
    curl.SetOpt(CURLoption.CURLOPT_VERBOSE, false);
    curl.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

    curl.Perform();
    curl.Cleanup();
    curl.GlobalCleanup(); 

    string result = sourceContent.ToString();

----------


public Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
  string aa = System.Text.Encoding.UTF8.GetString(buf);
  this.sourceContent.Append(aa + " ");
  return size * nmemb;
}

我认为这个问题是基于CURLOPT_HTTPHEADER。我认为sl (SList) 不是真的,或者我错过了添加一些内容。我上面的所有信息。

【问题讨论】:

  • 您是否检查过 Fiddler 或 Wireshark 等工具发送的实际请求?提琴手:fiddler2.com/fiddler2 和 Wireshark:wireshark.org
  • 我做到了,但我无法读取任何内容,因为帖子值已编码。我得到了一些不同的字符,例如正方形
  • 我知道 Fiddler 可以对很多常见的请求进行编码和解码。
  • fiddler 无法获取任何帖子值 :) 这是 fiddler 的图片。 img571.imageshack.us/img571/7092/5b20a23e2c3a428fa5230e4.png 如你所见,没有关于我的帖子的信息
  • 是的,我明白了。只是为了帮助调试将 url 更改为端口 80 并使用 fiddler 再次尝试。下一项是不使用环回地址。过去我在使用 fiddler 时遇到了一些环回和非 http 端口的问题。

标签: c# curl http-status-code-400 libcurl.net


【解决方案1】:

根据 libcurl.net 文档,它说不要将 CRLF 添加到您传递给 CURLOPT_HTTPHEADER 的标头中,因为 libcurl.net 已经在请求中添加了 CRLF。您可能会遇到服务器无法正确获取某些标头值的问题。此外,不要将请求行添加到包含 POST 或 GET 的标头中。请参阅下面的文档摘录:

Slist 中包含的标头不得以 CRLF 结尾,因为 curl 在每个标题项之后添加 CRLF。未能遵守此规定 将导致奇怪的错误,因为服务器很可能会忽略 您指定的部分标题。

请求的第一行 (通常包含 GET 或 POST)不是标头,也不能是 使用此选项替换。只有请求行之后的行 是标题。

【讨论】:

    猜你喜欢
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    相关资源
    最近更新 更多