400 Bad Request in Curl

4.2k Views Asked by At

For several days, I have been trying to post to a server. But I get a bad request error which is:

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 

my code is here (I used libcurl.net on c#)

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;
}

I think that problem is based on CURLOPT_HTTPHEADER. I think that sl (SList) is not true or I've missed to add something. All information what I have above.

1

There are 1 best solutions below

0
Cameron Tinker On

According to the libcurl.net documentation, it says to not add CRLF to your headers that you pass to CURLOPT_HTTPHEADER since libcurl.net already adds CRLF with the request. You may be having an issue with the server not correctly picking up some of your header values. Also, don't add the request-line to your headers containing POST or GET. See below for an excerpt from the documentation:

The headers included in the Slist must not be CRLF-terminated, because curl adds CRLF after each header item. Failure to comply with this will result in strange bugs because the server will most likely ignore part of the headers you specified.

The first line in a request (usually containing a GET or POST) is not a header and cannot be replaced using this option. Only the lines following the request-line are headers.