0

I am building an application in Delphi 2006 to integrate with Amazon's MWS API but am getting Socket Error 10061 Connection Refused.

Here is my Delphi code:

// pull out url end point - in my case mws-eu.amazonservices.com:443
url := '';
url := getOption('URL');
if url='' then LogMessage('DEV','No Amazon URL in INI file! ', true);

request := TStringList.Create;

// add standard required fields
//request.Add('Marketplace='+getOption('Marketplace')); 
request.Add('Action='+action);
request.Add('AWSAccessKeyId='+getOption('AWSAccessKeyId'));
//request.Add('MWSAuthToken='+getOption('MWSAuthToken')); 

//request.Add('Merchant='+getOption('SellerId'));
request.Add('SellerId='+getOption('SellerId'));
request.Add('SignatureMethod='+getOption('SignatureMethod')); // HmacSHA256
request.Add('SignatureVersion='+getOption('SignatureVersion')); // 2

request.Add('Version='+getOption('Version')); // not sure where this comes from or whether its common to all calls ?? Version=2009-01-01

// add request specific params (these are sent in to this function)
for loop := 0 to params.Count-1 do
begin
request.Add(paramsloop);
end;

// add timestamp
d:=Now;
dt:=FormatDateTime('yyyy-mm-dd"T"hh:mm:ss"Z"',d);
request.Add('Timestamp='+dt);

// call our custom sort method
LogMessage('DEV','Before='+request.GetText, false);
request.CustomSort(StringListCompareLogical);
LogMessage('DEV','After='+request.GetText, false);

// encode the params as per MWS
stringResult:=MWSEncodeParams(request);
LogMessage('DEV','result:'+stringResult, false);

stringToSign := 'POST' + char(10);
stringToSign := stringToSign + url + char(10);
stringToSign := stringToSign + '/' + char(10);
stringToSign := stringToSign + stringResult;

LogMessage('DEV','stringToSign:'+stringToSign, false);

// call sha method in DLL encrypter
uPugwash.getSH256_HMAC(getOption('SecretKey'),stringResult,True,Signature);
// add result to request as signature
request.Add('Signature='+Signature);
stringResult:=stringResult+'&Signature='+Signature;

// call md5 method in DLL encrypter
uPugwash.getMD5(requestBody,true,MD5) ;

LogMessage('DEV','output signature: '+Signature, true);
LogMessage('DEV','output md5: '+MD5, true);
lHTTP := TIdHTTP.Create(nil);

lIOHandler:=TIdSSLIOHandlerSocketOpenSSL.Create(nil);
lIOHandler.Port := 443; // lIOHandler.Port := 25;

try
lHTTP.IOHandler := lIOHandler;
lHTTP.ConnectTimeout:=5000;
lHTTP.HandleRedirects := True;
lHTTP.ReadTimeout := 20000;
lHTTP.Request.Method := 'POST';
lHTTP.Request.AcceptCharSet := 'UTF-8';
lHTTP.Request.ContentType := 'text/xml';
//lHTTP.Request.ContentType:='application/x-www-form-urlencoded';
lHTTP.Request.ContentEncoding := 'utf-8';
lHTTP.Request.UserAgent := 'POS Amazon Web Integration/'+getOption('posVersion')+' (Language=Delphi/2006; Customer='+getOption('Customer')+')';
lHTTP.Request.Host := url;
lHTTP.Request.CustomHeaders.Add('X-Amazon-User-Agent: '+lHTTP.Request.UserAgent);
lHTTP.Request.CustomHeaders.Add('Content-MD5: '+MD5);
lHTTP.Request.Accept:='text/plain, */*';
lHTTP.ProtocolVersion:=pv1_1;
lHTTP.HTTPOptions:=lHTTP.HTTPOptions+hoKeepOrigProtocol-hoForceEncodeParams;

RBody := TStringStream.Create(requestBody); // this is my feed xml
RBody.Seek(0,0);

LogMessage('DEV','request body:'+requestBody, false);
LogMessage('DEV','request url:'+stringResult, false);

fullURL := 'https://'+url+'?'+stringResult; // this is url with required parameters on end
LogMessage('DEV','fullURL:'+fullURL, false);

// make actual call
rawResp := lHTTP.POST(fullURL,RBody); // falls over here with socket exception

I have presumed above that the normal params are sent in the url and the xml is sent in the body.

Any pointers to where I may be going wrong would be much appreciated.

Thank you!

PaulG
  • 13,871
  • 9
  • 56
  • 78
  • 2
    `WSAECONREFUSED` means you tried to connect to a Host/Port that is not actively listening for connections, or a firewall/router is rejecting the connection. You are also doing things in your `TIdHTTP` setup that you should not be doing. Do not set `lIOHandler.Port`, `Request.Method`, `Request.ContentEncoding`, or `Request.Host` at all, `TIdHTTP` manages them for you. And you can drop `:443` from the url since that is the default for `https://`. – Remy Lebeau Nov 05 '14 at 18:11
  • Made those changes but still getting the same 10061 socket error. I have read elsewhere that adding :443 to the end of the endpoint is required when using Indy or the signature will not match, so have tried it with and without. When not used I get Socket Error #11004 - can I read anything into that? – Andy Baptiste Nov 06 '14 at 10:32
  • 11004 is `WSANO_DATA`, which is a DNS lookup error, it has nothing to do with port numbers. – Remy Lebeau Nov 06 '14 at 17:44

1 Answers1

0

I've been struggling with Indy and Amazon web services a lot too. The particular error you are getting is winsock error :

WSAECONNREFUSED
10061
Connection refused.No connection could be made because the target computer actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host—that is, one with no server application running.

Acoording to my experience the error may happen due to several things:

  1. Wrong server adress or wrong port - check the url and the port for the service. Make sure they are correct(I am no expert in MWS so can't help you with that)

  2. Some firewall on the client side is blocking the request - try to disable all kinds of firewall and try requesting again

  3. A very unstable internet connection on the client side can cause this error

All in all, I wouldn't encourage you to use Indy as I've seen it fail in many situations. I found out that the WinHTTP API altough a little bit more complex is way more stable.

Hope that some of this helps.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    What kind of problems are you having exactly? Indy works just fine when used correctly. – Remy Lebeau Nov 05 '14 at 18:13
  • 1
    `WinHTTP is way more stable` - WinHTTP also is way less cross-platform. Indy supports all Delphi target platforms, and works with Free Pascal (including Linux). – mjn Nov 06 '14 at 10:34