2013-10-28 138 views
-2

我試圖發送和德爾福GET HTTP響應

我使用的WinInet

這裏獲得德爾福從URL響應功能,我使用

編輯:我可以成功地發送和得到響應的問題是網站無法識別edt1發送的數據並重播無效響應我已經發布了下面的代碼工作完美的代碼工作代碼vb10代碼和我的地方有什麼區別或我做錯了?

in button1 

    var 
    s:= string; 
    begin 
    s:= GetUrlContent('website url ' + edt1.text); 
    memo.lines.add(s); 
    end; 



function GetUrlContent(const Url: string): string; 
    var 
     NetHandle: HINTERNET; 
     UrlHandle: HINTERNET; 
     Buffer: array[0..1024] of Char; 
     BytesRead: dWord; 
    begin 
     Result := ''; 
     NetHandle := InternetOpen('Delphi 5.x', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); 

     if Assigned(NetHandle) then 
     begin 
     UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0); 

     if Assigned(UrlHandle) then 
      { UrlHandle valid? Proceed with download } 
     begin 
      FillChar(Buffer, SizeOf(Buffer), 0); 
      repeat 
      Result := Result + Buffer; 
      FillChar(Buffer, SizeOf(Buffer), 0); 
      InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead); 
      until BytesRead = 0; 
      InternetCloseHandle(UrlHandle); 
     end 
     else 
      { UrlHandle is not valid. Raise an exception. } 
      raise Exception.CreateFmt('Cannot open URL %s', [Url]); 

     InternetCloseHandle(NetHandle); 
     end 
     else 
     { NetHandle is not valid. Raise an exception } 
     raise Exception.Create('Unable to initialize'); 
    end; 

但是當我的友情讓VB10與該 完美運行

這裏是他在VB

Dim httpWebRequest As HttpWebRequest = CType(WebRequest.Create("web-site url" + TextBox2.Text), HttpWebRequest) 
httpWebRequest.ContentType = TextBox2.Text.Trim() 
Dim httpWebResponse As HttpWebResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse) 
Dim streamReader As StreamReader = New StreamReader(httpWebResponse.GetResponseStream()) 
Dim text As String = streamReader.ReadToEnd() 
Richtextbox1.text = text 

我在做什麼錯誤的代碼示例相同的請求?

+0

你將格式不正確的URL傳遞給您的函數。 's:= GetUrlContent('website url'+ edt1.text);'應該是':= GetUrlContent(edt1.Text)' –

+0

是的,我正在做對 - 我必須發送url + edt1.text s:= geturlcontent('這裏去網站的網址'+ edt1.text); //它的url + edt1.text – dudey

+0

然後在你的問題中陳述,所以我們不要猜測。你使用哪個Delphi版本? –

回答

2

很抱歉,但我不知道什麼是你的代碼錯誤,但如果你的最終目標是從網頁內容加載到一個備忘錄時,爲什麼不直接使用

Uses IdHTTP; 

function getContent(url: String): String; 
var 
http : TIdHTTP; 
begin 
http := TIdHTTP.Create(nil); 
    try 
    Result := http.Get(url); 
    finally 
    http.Free; 
    end; 
end; 

Memo.Lines.Add(getContent('http://websiteurl.com')); 
+0

thx大家幫忙 經過很多搜索 我的問題是從這個線程解決 http:// stackoverflow。com/questions/17856133/how-can-i-use-tidhttp-to-make-a-get-request-with-a-parameter-called-xml – dudey

+0

當它運行時它似乎凍結了..可能太多了網站? –