* * *

Author Topic: [SOLVED] Synapse - 400 Bad Request  (Read 11895 times)

szocsmarci

  • New member
  • *
  • Posts: 8
[SOLVED] Synapse - 400 Bad Request
« on: April 07, 2011, 10:28:04 pm »
I want to get html from a page with Synapse. It works fine with many pages, but somewhere it doesn't.

It says:
HTTP/1.1 400 Bad Request
Date: Thu, 07 Apr 2011 19:57:37 GMT
Server: Apache
Content-Length: 344
Connection: close
Content-Type: text/html; charset=iso-8859-1

Your browser sent a request that this server could not understand.


Here is the code (Button1, Edit1, Memo1 on the form):
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  HTTP: THTTPSend;
  l: tstringlist;
begin
  HTTP := THTTPSend.Create;
     l := TStringList.create;
  try
    if not HTTP.HTTPMethod('GET', edit1.Text) then
      begin
ShowMessage('ERROR' + sLineBreak + IntToStr(Http.Resultcode));
      end
    else
      begin
        memo1.Lines.Clear;
        memo1.Lines.Append(inttostr(Http.Resultcode) + ' ' + Http.Resultstring);
        memo1.Lines.Append(Http.headers.text);
        l.loadfromstream(Http.Document);
        memo1.Lines.Append(l.text);
      end;
  finally
    HTTP.Free;
    l.free;
  end;
  end;  

But it is the same with synapse HttpGetText and lnet too.

I think I use wrong function (HTTP instead of HTTPs or something else)
« Last Edit: April 08, 2011, 08:58:11 pm by szocsmarci »

Dibo

  • Hero Member
  • *****
  • Posts: 1045
Re: Synapse - 400 Bad Request
« Reply #1 on: April 07, 2011, 10:47:01 pm »
If your interner browser shows page with http/https then this components should too. This is all code?
- Maybe server do some redirection. Synapse have property "AllowRedirection" or something like that. Check if this is TRUE
- I notify that some servers are sensitive for line breaks. I have similar problems with some servers when I separate headers line using unix #10. Try with #13#10
- Synapse default add some standards headers (like content-type, length etc) but lNET add only "host". Maybe server get headers which he doesn't understand or get not enough headers (LNet).

The best solution is run some sniffer (I use wireshark on linux) and look what send synapse/lNet and what internet browser and try send this same headers (in lNet you do this by LHTTPClient.AddExtraHeaders('Content-type: text/html; charset=iso-8859-1'))

Regards

ik

  • Jr. Member
  • **
  • Posts: 88
  • ik
    • LINESIP
Re: Synapse - 400 Bad Request
« Reply #2 on: April 08, 2011, 01:03:25 am »
Bad request usually means that the content of what you sent was not what the HTTP server/cgi expected.
All the 4xx errors are based on problems by the client side (wrong page, forbidden, etc...)

It can be not the whole header, wrong header (what is the content of Edit1.text that you are using ?)

If you can provide us the whole header (you can remove the IP/dns), it can be more helpful to understand what might have gone wrong in your request.
You can do it by using tools such as Wireshark.

Quote from: dibo
- Maybe server do some redirection.
Redirect is a 3xx based "error" code. It can have code of "Moved Permanently", and it can have "Temporary Redirect".

Almindor

  • Sr. Member
  • ****
  • Posts: 411
    • http://www.astarot.tk
Re: Synapse - 400 Bad Request
« Reply #3 on: April 08, 2011, 09:50:29 am »
This can happen if you go to a https page without the SSL.

Try lNEt with SSL turned on (use https).

szocsmarci

  • New member
  • *
  • Posts: 8
Re: Synapse - 400 Bad Request
« Reply #4 on: April 08, 2011, 03:00:29 pm »
Quote
This can happen if you go to a https page without the SSL.
I turned on the SSL, but nothing hapenned. (lNET - SSL - properties)

I examined the traffic with Wireshark. It occured to me that Firefox sended cookies, while synapse didn't. But when I turned off the cookies in FF it worked. So it doesn't depends on cookies (I think).

I tried to extract the sended objects.

Synapse - doesn't work
GET /akt.php HTTP/1.1

FF - works
GET /akt.php HTTP/1.1
Host: www.here_comes_the_web_address.hu
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: hu-hu,hu;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cache-Control: max-age=0

And one more thing. When I type the IP number ((http://123.45.67.8/ instead of the name) in FF, I get the same error.
« Last Edit: April 08, 2011, 08:59:33 pm by szocsmarci »

jixian.yang

  • Full Member
  • ***
  • Posts: 173
Re: Synapse - 400 Bad Request
« Reply #5 on: April 08, 2011, 06:25:35 pm »
To forbid visit apache web site via IP address, by adding the following code into the end of httpd.conf:

     NameVirtualHost 195.70.35.9
     <VirtualHost 195.70.35.9>
     ServerName 195.70.35.9
     <Location />
        Order Allow,Deny
        Deny from all
     </Location>
     </VirtualHost>    
     <VirtualHost 195.70.35.9>
     DocumentRoot "c:/web (or another path)"
     ServerName www.tippmix.hu
     </VirtualHost>

and restart Apache,
then the web site can only be visited by domain name.

ik

  • Jr. Member
  • **
  • Posts: 88
  • ik
    • LINESIP
Re: Synapse - 400 Bad Request
« Reply #6 on: April 08, 2011, 07:33:06 pm »
Quote
This can happen if you go to a https page without the SSL.
I turned on the SSL, but nothing hapenned. (lNET - SSL - properties)

I examined the traffic with Wireshark. It occured to me that Firefox sended cookies, while synapse didn't. But when I turned off the cookies in FF it worked. So it doesn't depends on cookies (I think).

I tried to extract the sended objects.

Synapse - doesn't work
GET /akt.php HTTP/1.1

FF - works
GET /akt.php HTTP/1.1
Host: www.tippmix.hu
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: hu-hu,hu;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cache-Control: max-age=0

And one more thing. When I type the IP number ((http://195.70.35.9/ instead of the name) in FF, I get the same error.


Synapse does HTTP 1.0 request while you expect 1.1 .
It's not only the declaration of HTTP 1.1, but you must supply additional information, such as cookies, Accept-Chars and few others.

Please take a look at how I did it in my code here. It uses LNet but it does not matter what library you are using as long as it can send all of the required headers.

szocsmarci

  • New member
  • *
  • Posts: 8
Re: Synapse - 400 Bad Request
« Reply #7 on: April 08, 2011, 08:57:11 pm »
Thanks all of You, it is solved.

That was a need of a header row namely: Accept: text/html

The whole procedure is:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  HTTP: THTTPSend;
  l: tstringlist;
  Header : TStringList;  // + row !!!!!!!!!!!
begin
  Header := TStringList.Create; // + row !!!!!!!!!!!
  HTTP := THTTPSend.Create;
  l := TStringList.create;
  Header.Add('Accept: text/html');  // + row !!!!!!!!!!!
  HTTP.Headers.AddStrings(Header);  // + row !!!!!!!!!!!
  try
    if not HTTP.HTTPMethod('GET', edit1.Text) then
      begin
ShowMessage('ERROR' + sLineBreak + IntToStr(Http.Resultcode));
      end
    else
      begin
        memo1.Lines.Clear;
        memo1.Lines.Append(inttostr(Http.Resultcode) + ' ' + Http.Resultstring);
        memo1.Lines.Append(Http.headers.text);
        l.loadfromstream(Http.Document);
        memo1.Lines.Append(l.text);
      end;
  finally
    HTTP.Free;
    Header.Free;  // + row !!!!!!!!!!!
    l.free;
  end;
  end;             

That is all.
The Wireshark is very useful program, I didn't even heard about such program.
Again thanks all of You.

CM630

  • Hero Member
  • *****
  • Posts: 814
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: [SOLVED] Synapse - 400 Bad Request
« Reply #8 on: May 19, 2018, 11:27:27 am »
I came across a 400 Bad Request issue yesterday.
Web browser was opening the item just fine, but Lazarus did not.

In case it would be useful for s.o. - it occurred that I was trying to open the address with space characters instead of %20. Respectfully there is some other character which shall be used with %40.
Лазар 1,8,0;W7 64bit or XP 32bit;FPC3,0,4;rev 56594

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus