2

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

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) 不正确,或者我错过了添加一些内容。我上面的所有信息。

4

1 回答 1

1

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

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

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

于 2013-11-11T14:37:22.970 回答