我正在使用Delphi 2006构建一个应用程序,以便与Amazon的MWS API集成,但是套接字错误10061连接被拒绝。
这是我的Delphi代码:
// 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
我已经在上面假设,正常的参数在网址中发送,xml在正文中发送。
任何指向我可能出错的地方都会非常感激。
谢谢!
答案 0 :(得分:0)
我也一直在努力与Indy和亚马逊网络服务竞争。您遇到的特定错误是winsock错误:
WSAECONNREFUSED
10061个
连接被拒绝。由于目标计算机主动拒绝连接,因此无法建立连接。这通常是因为尝试连接到外部主机上处于非活动状态的服务,即没有运行服务器应用程序的服务。
根据我的经验,错误可能由于以下几点而发生:
错误的服务器地址或错误的端口 - 检查服务的URL和端口。确保它们是正确的(我不是MWS的专家,所以可以帮助你)
客户端的某些防火墙阻止了请求 - 尝试禁用所有类型的防火墙并再次尝试请求
客户端非常不稳定的互联网连接可能会导致此错误
总而言之,我不鼓励你使用Indy,因为我已经看到它在很多情况下失败了。我发现WinHTTP API虽然稍微复杂一点,但更稳定。
希望其中一些有所帮助。