TScHttpWebRequest - Exception
Posted: Wed 08 Dec 2021 14:03
Hi,
I cannot get the exact server message of exception in the 2nd URL call. the below is my code.
1) First URL call no error:
try
Try
vHttpWebRequest := TScHttpWebRequest.Create(URL1);
vHttpWebRequest.Method := rmGET;
vHttpWebRequest.Accept := 'application/json';
vHttpWebRequest.ContentType := 'application/json';
vHttpWebRequest.KeepAlive := True;
vWebResponse := vHttpWebRequest.GetResponse;
vResponseStrRequest := vWebResponse.ReadAsString;
Except
on E : HttpException do
begin
vTmpStr := 'Error Code: ' + IntToStr(E.Code) + ' | ' +
'Server Message: ' + E.ServerMessage + ' | ' +
'Message: ' + E.Message;
raise Exception.Create(vTmpStr);
end;
on E: Exception do
begin
raise Exception.Create(E.Message);
end;
End;
Finally
vHttpWebRequest.Free;
end;
2) 2nd URL call has error, but cannot get the exact server message.
try
Try
vHttpWebRequest := TScHttpWebRequest.Create(URL2);
vHttpWebRequest.Method := rmPOST;
vHttpWebRequest.Accept := 'application/json';
vHttpWebRequest.ContentType := 'application/json';
vHttpWebRequest.KeepAlive := True;
vBuffer := TEncoding.UTF8.GetBytes(xxxxxxxxxxxxx);
vHttpWebRequest.ContentLength := Length(vBuffer);
vHttpWebRequest.WriteBuffer(vBuffer);
vWebResponse := vHttpWebRequest.GetResponse;
vResponseStrRequest := vWebResponse.ReadAsString;
Except
on E : HttpException do
begin
vTmpStr := 'Error Code: ' + IntToStr(E.Code) + ' | ' +
'Server Message: ' + E.ServerMessage + ' | ' +
'Message: ' + E.Message;
raise Exception.Create(vTmpStr);
end;
on E: Exception do
begin
raise Exception.Create(E.Message);
end;
End
Finally
vHttpWebRequest.Free;
end;
If i use Postman to test the URL2, it can return the server message 'Error: xxxxxxxxxxxxxxxxxx'.
However, if i run the my program to call the URL, it return other error message.
(if I only run the 2nd URL call, then can get the exact server message)
what is wrong in my code?
Thank you.
I cannot get the exact server message of exception in the 2nd URL call. the below is my code.
1) First URL call no error:
try
Try
vHttpWebRequest := TScHttpWebRequest.Create(URL1);
vHttpWebRequest.Method := rmGET;
vHttpWebRequest.Accept := 'application/json';
vHttpWebRequest.ContentType := 'application/json';
vHttpWebRequest.KeepAlive := True;
vWebResponse := vHttpWebRequest.GetResponse;
vResponseStrRequest := vWebResponse.ReadAsString;
Except
on E : HttpException do
begin
vTmpStr := 'Error Code: ' + IntToStr(E.Code) + ' | ' +
'Server Message: ' + E.ServerMessage + ' | ' +
'Message: ' + E.Message;
raise Exception.Create(vTmpStr);
end;
on E: Exception do
begin
raise Exception.Create(E.Message);
end;
End;
Finally
vHttpWebRequest.Free;
end;
2) 2nd URL call has error, but cannot get the exact server message.
try
Try
vHttpWebRequest := TScHttpWebRequest.Create(URL2);
vHttpWebRequest.Method := rmPOST;
vHttpWebRequest.Accept := 'application/json';
vHttpWebRequest.ContentType := 'application/json';
vHttpWebRequest.KeepAlive := True;
vBuffer := TEncoding.UTF8.GetBytes(xxxxxxxxxxxxx);
vHttpWebRequest.ContentLength := Length(vBuffer);
vHttpWebRequest.WriteBuffer(vBuffer);
vWebResponse := vHttpWebRequest.GetResponse;
vResponseStrRequest := vWebResponse.ReadAsString;
Except
on E : HttpException do
begin
vTmpStr := 'Error Code: ' + IntToStr(E.Code) + ' | ' +
'Server Message: ' + E.ServerMessage + ' | ' +
'Message: ' + E.Message;
raise Exception.Create(vTmpStr);
end;
on E: Exception do
begin
raise Exception.Create(E.Message);
end;
End
Finally
vHttpWebRequest.Free;
end;
If i use Postman to test the URL2, it can return the server message 'Error: xxxxxxxxxxxxxxxxxx'.
However, if i run the my program to call the URL, it return other error message.
(if I only run the 2nd URL call, then can get the exact server message)
what is wrong in my code?
Thank you.