Http Post(200分)

  • 主题发起人 主题发起人 地质灾害
  • 开始时间 开始时间

地质灾害

Unregistered / Unconfirmed
GUEST, unregistred user!
一段调用WinInet API进行Http Post的代码 向本地的Web服务器Post成功 往外网的服务器Post就不成功 良久找不到原因。求助各位大虾。代码如下<br><br>function ParseUrl(const aUrl:string;var aHost,aFile:string;var aPort:Integer):Boolean;<br>var<br>&nbsp; i: Integer;<br>&nbsp; S:string;<br>begin<br>&nbsp; Result:=False;<br>&nbsp; if aUrl='' then Exit;<br>&nbsp; if FastPos('http://', LowerCase(aUrl),1) &lt;&gt; 0 then<br>&nbsp; &nbsp; S:=Copy(aURL, 8,Length(aUrl)-7)<br>&nbsp; else &nbsp;if FastPos('https://', LowerCase(aUrl),1) &lt;&gt; 0 then<br>&nbsp; &nbsp; S:=Copy(aURL, 9,Length(aUrl)-8)<br>&nbsp; else S:=aUrl;<br>&nbsp; if S='' then Exit;<br>&nbsp; i := FastPos('/',S,1);<br>&nbsp; if i=1 then Exit;<br>&nbsp; if i=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; aFile:='/';<br>&nbsp; &nbsp; aHost:=S;<br>&nbsp; end<br>&nbsp; else if i=1 then Exit<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; aHost := Copy(S, 1, i - 1);<br>&nbsp; &nbsp; aFile := Copy(S, i, Length(S) - i + 1);<br>&nbsp; end;<br>&nbsp; i := FastPos(':',aHost,1);<br>&nbsp; if i &gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; if (i=1) or (i=Length(aHost)) then Exit;<br>&nbsp; &nbsp; if not StrToIntDef(Copy(aHost, i + 1, Length(aHost) - i),aPort) then Exit;<br>&nbsp; &nbsp; Delete(aHost, i, Length(aHost)-i+1);<br>&nbsp; end<br>&nbsp; else aPort:=80;<br>&nbsp; if (aHost[Length(aHost)] = '/') then<br>&nbsp; &nbsp; Delete(aHost, Length(aHost),1);<br>&nbsp; Result:=True;<br>end;<br><br>function IntToStr(Value: Integer): string;<br>asm<br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;ESI<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; ESI, ESP<br>&nbsp; &nbsp; &nbsp; &nbsp; SUB &nbsp; &nbsp; ESP, 16<br>&nbsp; &nbsp; &nbsp; &nbsp; XOR &nbsp; &nbsp; ECX, ECX &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; PUSH &nbsp; &nbsp;EDX<br>&nbsp; &nbsp; &nbsp; &nbsp; XOR &nbsp; &nbsp; EDX, EDX<br>&nbsp; &nbsp; &nbsp; &nbsp; CALL &nbsp; &nbsp;CvtInt<br>&nbsp; &nbsp; &nbsp; &nbsp; MOV &nbsp; &nbsp; EDX, ESI<br>&nbsp; &nbsp; &nbsp; &nbsp; POP &nbsp; &nbsp; EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; CALL &nbsp; &nbsp;System.@LStrFromPCharLen<br>&nbsp; &nbsp; &nbsp; &nbsp; ADD &nbsp; &nbsp; ESP, 16<br>&nbsp; &nbsp; &nbsp; &nbsp; POP &nbsp; &nbsp; ESI<br>end;<br><br>procedure UrlHttpPost(aUrl:string;aPostData:string);<br>var<br>&nbsp; aHost,aFile:string;<br>&nbsp; aPort:Integer;<br>&nbsp; hSession,hConnection,hRequest: HINTERNET;<br>&nbsp; mimeType:PAnsiChar;<br>begin<br>&nbsp; if not ParseUrl(aUrl,aHost,aFile,aPort) then Exit;<br>&nbsp; hSession:=InternetOpen(nil,INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);<br>&nbsp; if hSession=nil then<br>&nbsp; begin<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; hConnection:=InternetConnect(hSession,PChar(aHost),aPort,nil,nil,INTERNET_SERVICE_HTTP,0,0);<br>&nbsp; if hConnection=nil then<br>&nbsp; begin<br>&nbsp; &nbsp; InternetCloseHandle(hSession);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; mimeType:='Accept: *.*';<br>&nbsp; hRequest:=HttpOpenRequest(hConnection,'POST',PChar(aFile),'HTTP/1.1',nil,@mimeType,INTERNET_FLAG_RELOAD,0);<br>&nbsp; if hRequest=nil then<br>&nbsp; begin<br>&nbsp; &nbsp; InternetCloseHandle(hConnection);<br>&nbsp; &nbsp; InternetCloseHandle(hSession);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; if HttpSendRequest(hRequest,'Content-Type: application/x-www-form-urlencoded',47,PAnsiChar(aPostData),Length(aPostData)) then<br>&nbsp; begin<br>&nbsp; &nbsp; InternetCloseHandle(hRequest);<br>&nbsp; &nbsp; InternetCloseHandle(hConnection);<br>&nbsp; &nbsp; InternetCloseHandle(hSession);<br>&nbsp; end;<br>end;
 
代码再多点,可以帮你测试一下
 
procedure WebBrowserPost(const objBrowser:IWebBrowser2;aUrl:string;aData:string);<br>var<br>&nbsp; PostData: OleVariant;<br>&nbsp; Headers: OleVariant;<br>&nbsp; i: Integer;<br>&nbsp; Flags:OleVariant;<br>&nbsp; TargetFrameName:OleVariant;<br>begin<br>&nbsp; PostData := VarArrayCreate([0, Length(aData) - 1], varByte);<br>&nbsp; for i := 1 to Length(aData) do<br>&nbsp; &nbsp; PostData[i-1] := Ord(aData);<br>&nbsp; Headers := 'Content-Type: application/x-www-form-urlencoded' + #10#13;<br>&nbsp; Flags:=0;<br>&nbsp; TargetFrameName:=NULL;<br>&nbsp; objBrowser.Navigate(aUrl,Flags,TargetFrameName,PostData,Headers);<br>end;<br>用这个搞定了。谢谢楼上。也许是WinInet API的BUG吧。[:D]
 

Similar threads

I
回复
0
查看
831
import
I
I
回复
0
查看
541
import
I
I
回复
0
查看
657
import
I
后退
顶部