In my recent work i have to send a http get request to the itunes app store.In the past i send the request by idhttp compents,but i notice that when get something vi openssl i get the wrong data.there are some noise data,just as below:
1 2 3 | <view alt="" stretchiness="1"></view>
<gotourl target="main" inhibitDragging="true" url="https://st11p08it-finance004/WebObjects/MZFinance.woa/wo/0.0.0.0.0.2.1.1.1.47.1.7.0.1.0.1.0.0.5.0.15.1.0.3.0.1.1.3.0.7.7.0.5.1.0.3.0.5.0.0.1.3.0.0.3.0" formViewName="giftAlbumForm"><picturebuttonview leftInset="3" width="14" topInset="1" picts="plain,pressed" rightInset="3" alt="selectedSend gift via email" url="https://s.mzstatic.com/images/btn_radio_selected.png" height="15"></picturebuttonview></gotourl>
<view alt="" stretchiness="1"></view> |
but the correct data should be like this:
1 2 3 | <view alt="" stretchiness="1"></view>
<gotourl target="main" inhibitDragging="true" url="https://WebObjects/MZFinance.woa/wo/2.0.0.0.0.2.1.1.1.47.1.7.0.1.0.1.0.0.5.0.15.1.0.3.0.1.1.3.0.7.7.0.5.1.0.3.0.5.0.0.1.3.0.0.3.0" formViewName="giftAlbumForm"><picturebuttonview leftInset="3" width="14" topInset="1" picts="plain,pressed" rightInset="3" alt="selectedSend gift via email" url="https://s.mzstatic.com/images/btn_radio_selected.png" height="15"></picturebuttonview></gotourl>
<view alt="" stretchiness="1"></view> |
with no “st11p08it-finance004/” and “0.0.0.0.0.2.1.1.1.47.1.7.0.1.0.1.0.0.5.0.15.1.0.3.0.1.1.3.0.7.7.0.5.1.0.3.0.5.0.0.1.3.0.0.3.0” should be “2.0.0.0.0.2.1.1.1.47.1.7.0.1.0.1.0.0.5.0.15.1.0.3.0.1.1.3.0.7.7.0.5.1.0.3.0.5.0.0.1.3.0.0.3.0”
via the wininet api i get the correct result but i don’t know why there are so many diffrents .if anyone know just leave a message here .thx.:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | Function sslInet(Const AServer, AUrl, AData : AnsiString; blnSSL:Boolean = True): AnsiString; var aBuffer : Array[0..4096] of Char; Header : TStringStream; BufStream : TMemoryStream; sMethod : AnsiString; BytesRead : Cardinal; pSession : HINTERNET; pConnection : HINTERNET; pRequest : HINTERNET; begin Result := ''; pSession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); if Assigned(pSession) then try case blnSSL of True : pConnection := InternetConnect(pSession, PChar(AServer), INTERNET_DEFAULT_HTTPS_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0); False : pConnection := InternetConnect(pSession, PChar(AServer), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0); end; if Assigned(pConnection) then try if (AData = '') then sMethod := 'GET' else sMethod := 'POST'; case blnSSL of True : pRequest := HTTPOpenRequest(pConnection, PChar(sMethod), PChar(AURL), nil, nil, nil, INTERNET_FLAG_SECURE or INTERNET_FLAG_KEEP_CONNECTION, 0); False : pRequest := HTTPOpenRequest(pConnection, PChar(sMethod), PChar(AURL), nil, nil, nil, INTERNET_SERVICE_HTTP, 0); end; if Assigned(pRequest) then try Header := TStringStream.Create(''); with Header do begin WriteString('Host: ' + AServer + sLineBreak); WriteString('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10'+SLineBreak); WriteString('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'+SLineBreak); WriteString('Accept-Language: en-us,en;q=0.5' + SLineBreak); WriteString('Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'+SLineBreak); WriteString('Keep-Alive: 300'+ SLineBreak); WriteString('Connection: keep-alive'+ SlineBreak+SLineBreak); end; HttpAddRequestHeaders(pRequest, PChar(Header.DataString), Length(Header.DataString), HTTP_ADDREQ_FLAG_ADD); if HTTPSendRequest(pRequest, nil, 0, Pointer(AData), Length(AData)) then begin BufStream := TMemoryStream.Create; try while InternetReadFile(pRequest, @aBuffer, SizeOf(aBuffer), BytesRead) do begin if (BytesRead = 0) then Break; BufStream.Write(aBuffer, BytesRead); end; aBuffer[0] := #0; BufStream.Write(aBuffer, 1); Result := PChar(BufStream.Memory); finally FreeAndNil(BufStream); end; end; finally InternetCloseHandle(pRequest); FreeAndNil(Header); end; finally InternetCloseHandle(pConnection); end; finally InternetCloseHandle(pSession); end; end; |
link:http://pastebin.com/f1ea3a752
And a update Version From here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | function request(const AUrl, AData: AnsiString; blnSSL: Boolean = True): AnsiString; var aBuffer : Array[0..4096] of Char; Header : TStringStream; BufStream : TMemoryStream; sMethod : AnsiString; BytesRead : Cardinal; pSession : HINTERNET; pConnection : HINTERNET; pRequest : HINTERNET; parsedURL : TStringArray; port : Integer; flags : DWord; begin ParsedUrl := ParseUrl(AUrl); Result := ''; pSession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); if Assigned(pSession) then try if blnSSL then Port := INTERNET_DEFAULT_HTTPS_PORT else Port := INTERNET_DEFAULT_HTTP_PORT; pConnection := InternetConnect(pSession, PChar(ParsedUrl[0]), port, nil, nil, INTERNET_SERVICE_HTTP, 0, 0); if Assigned(pConnection) then try if (AData = '') then sMethod := 'GET' else sMethod := 'POST'; if blnSSL then flags := INTERNET_FLAG_SECURE or INTERNET_FLAG_KEEP_CONNECTION else flags := INTERNET_SERVICE_HTTP; pRequest := HTTPOpenRequest(pConnection, PChar(sMethod), PChar(ParsedUrl[1]), nil, nil, nil, flags, 0); if Assigned(pRequest) then try Header := TStringStream.Create(''); try with Header do begin WriteString('Host: ' + ParsedUrl[0] + sLineBreak); WriteString('User-Agent: Custom program 1.0'+SLineBreak); WriteString('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'+SLineBreak); WriteString('Accept-Language: en-us,en;q=0.5' + SLineBreak); WriteString('Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'+SLineBreak); WriteString('Keep-Alive: 300'+ SLineBreak); WriteString('Connection: keep-alive'+ SlineBreak+SLineBreak); end; HttpAddRequestHeaders(pRequest, PChar(Header.DataString), Length(Header.DataString), HTTP_ADDREQ_FLAG_ADD); if HTTPSendRequest(pRequest, nil, 0, Pointer(AData), Length(AData)) then begin BufStream := TMemoryStream.Create; try while InternetReadFile(pRequest, @aBuffer, SizeOf(aBuffer), BytesRead) do begin if (BytesRead = 0) then Break; BufStream.Write(aBuffer, BytesRead); end; aBuffer[0] := #0; BufStream.Write(aBuffer, 1); Result := PChar(BufStream.Memory); finally BufStream.Free; end; end; finally Header.Free; end; finally InternetCloseHandle(pRequest); end; finally InternetCloseHandle(pConnection); end; finally InternetCloseHandle(pSession); end; end; |
原创文章,转载请注明: 转载自 obaby@mars
本文标题: 《Send a HTTP POST Request in Delphi using WinInet api》
本文链接地址: http://h4ck.org.cn/2012/03/send-a-http-post-request-in-delphi-using-wininet-api/