我正在尝试从 delphi 中的 url 发送和获取响应
我正在使用 WinInet
这是我正在使用的功能
编辑:我可以成功发送并获得响应问题是网站无法识别发送的 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
我做错了什么?