Question

I need to authorize via link "https://mysite.sexy/sessionid?login=abc&password=abc" and I got TidHttp and TIdSSLIOHandlerSocketOpenSSL connected both.

I send a query:

http.get("https://mysite.sexy/sessionid?login=abc&password=abc");

but instead of getting any response, after some seconds, I get this exception: "HTTP/1.1 401 Unauthorized."

I had a search over web and got these solutions:

http.Request.UserName :=  'abc';
http.Request.Password := 'abc';
http.Request.UserAgent := 'Mozilla/5.0 (X11; U; Linux i686; cs-CZ; rv:1.7.12) Gecko/20050929';

but still it doesn't help anyhow. However a response from a web-site via Chrome-browser doens't occur as an exception and just retrieve error code and description in JSON format:

{"Code":4,"Desc":"Invalid user login or password"}

So this response is what I'm waiting for, but get only exception and thread stops. Please, give directions! Delphi XE3.

Was it helpful?

Solution

This is normal for Indy. It throws exceptions for non-successful responses like 401. You need to catch the exception and then check the response text, if you're really interested in the response text. (Typically, you're not, because the response code alone tells you all you need to know.) When you get an exception, the response text will be in the exception's ErrorMessage property.

try
  x := http.get("https://mysite.sexy/sessionid?login=abc&password=abc");
except
  on E: EIdHTTPProtocolException do begin
    if E.ErrorCode = 401 then
      x := E.ErrorMessage
    else
      raise;
  end;
end;

Setting the authorization properties of the request object apparently doesn't help because your server doesn't use standard HTTP authentication. Otherwise, Indy would have handled the 401 response and resent the request with the password automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top