6
6713827
Unregistered / Unconfirmed
GUEST, unregistred user!
先看代码!<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, WinInet, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>//获取WEB信息<br>function PostURL(const aUrl: string;PostQuery: string=''): String;<br>var<br> hSession: HINTERNET;<br> hConnect, hRequest: HINTERNET;<br> lpBuffer: array[0..1024 + 1] of Char;<br> dwBytesRead: DWORD;<br> HttpStr: string;<br> HostName, FileName: string;<br> FTResult: Boolean;<br> AcceptType: LPStr;<br> Buf: Pointer;<br> dwBufLen, dwIndex: DWORD;<br> procedure ParseURL(URL: string; var HostName, FileName: string);<br> procedure ReplaceChar(c1, c2: Char; var St: string);<br> var<br> p: Integer;<br> begin<br> while True do begin<br> p := Pos(c1, St);<br> if p = 0 then Break<br> else St[p] := c2;<br> end;<br> end;<br> var<br> i: Integer;<br> begin<br> if Pos(UpperCase('http://'), UpperCase(URL)) <> 0 then<br> System.Delete(URL, 1, 7);<br> i := Pos('/', URL);<br> HostName := Copy(URL, 1, i);<br> FileName := Copy(URL, i, Length(URL) - i + 1);<br> if (Length(HostName) > 0) and (HostName[Length(HostName)] = '/') then<br> SetLength(HostName, Length(HostName) - 1);<br> end;<br>begin<br> Result := '';<br> hSession := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);<br> try<br> if Assigned(hSession) then begin<br> ParseURL(aUrl, HostName, FileName);<br> hConnect := InternetConnect(hSession, PChar(HostName),<br> INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);<br> AcceptType := PChar('*/*'); <br> If PostQuery<>'' Then<br> hRequest := HttpOpenRequest(hConnect, 'POST', PChar(FileName), 'HTTP/1.0',<br> nil, @AcceptType, INTERNET_FLAG_RELOAD, 0)<br> Else<br> hRequest := HttpOpenRequest(hConnect, 'GET', PChar(FileName), 'HTTP/1.0',<br> nil, @AcceptType, INTERNET_FLAG_RELOAD, 0);<br> HttpSendRequest(hRequest, 'Content-Type: application/x-www-form-urlencoded', 47,<br> PChar(PostQuery), Length(PostQuery));<br> dwIndex := 0;<br> dwBufLen := 1024;<br> GetMem(Buf, dwBufLen);<br> FTResult := HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH,<br> Buf, dwBufLen, dwIndex);<br> if FTResult = True then try<br> while True do begin<br> dwBytesRead := 1024;<br> InternetReadFile(hRequest, @lpBuffer, 1024, dwBytesRead);<br> if dwBytesRead = 0 then Break;<br> lpBuffer[dwBytesRead] := #0;<br> HttpStr := HttpStr + lpBuffer;<br> end;<br> Result := HttpStr;<br> finally<br> InternetCloseHandle(hRequest);<br> InternetCloseHandle(hConnect);<br> end;<br> end;<br> finally<br> InternetCloseHandle(hSession);<br> end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> showmessage(PostURL('http://www.aa137.com/ip.asp'));<br>end;<br><br>end.<br>按回车键80%都是提示"[red]<h1>Bad Request</h1>[/red]"<br>按空格键100%提示[red]124.236.2.109:5022[/red]<br>我用的是DELPHI2007,<br>大家有碰到过这个问题吗?