[SOLVED] Synapse - 400 Bad Request lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- szocsmarci · 2011-04-07 20:28 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) ---------------------------------------------------------------------- Dibo · 2011-04-07 20:47 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 · 2011-04-07 23:03 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 · 2011-04-08 07:50 This can happen if you go to a https page without the SSL. Try lNEt with SSL turned on (use https). ---------------------------------------------------------------------- szocsmarci · 2011-04-08 13:00 QuoteThis 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. ---------------------------------------------------------------------- jixian.yang · 2011-04-08 16:25 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            ServerName 195.70.35.9               Order Allow,Deny         Deny from all                            DocumentRoot "c:/web (or another path)"      ServerName www.tippmix.hu       and restart Apache, then the web site can only be visited by domain name. ---------------------------------------------------------------------- ik · 2011-04-08 17:33 Quote from: szocsmarci on April 08, 2011, 03:00:29 pmQuoteThis 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 · 2011-04-08 18:57 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 · 2018-05-19 09:27 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.