Socket.Request.ContentType := 'application/x-www-form-urlencoded';
Socket.Request.ContentType := 'application/x-www-form-urlencoded';
Socket.Request.UserAgent := "My agent name";
Pars := TStringList.Create;
Pars.Add('txtTarget=abc');
Pars.Add('&txtArea=area');
Pars.Add('&txtTitle=Robot');
Pars.Add('&txtPageNr=1');
Pars.Add('&txtItemsPage=10');
Res := TStringStream.Create('');
Socket.Post(URL, Pars, Res);
Where Socket is a TIdHTTP.
noErrors:= True;
intReadTimeout:= 1 * 30 * 1000;
try // --> First try //* Creating the download object
IdHTTP:= TIdHTTP.Create(nil);
IdHTTP.ReadTimeout:= intReadTimeout; //* Creating an URI object that helps extracting the filename
URI:= TidURI.Create(TargetURI); //********** //* Setting the correct port if it was defined, otherwise it is 80 by default
if URI.Port <> '' then
IdHTTP.Port:= StrToInt(URI.Port); //* Some sites may not respond properly if the useragent variable isn't set correctly IdHTTP.Request.UserAgent:= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
IdHTTP.HandleRedirects:= True;
IdHTTP.OnWork:= OnWorkHandler; //********** //* Get the page, load into parser memory IdHTTP.Get(TargetURI, pageStream);
except //* Allow application to show current site
if Assigned(Settings.pReportCurrentURI) then
Settings.ReportCurrentURI(TargetURI + ' - second try '); try // --> Second try
IdHTTP.Free; IdHTTP:= nil; IdHTTP:= TIdHTTP.Create(nil);
IdHTTP.ReadTimeout:= intReadTimeout; if URI.Port <> '' then IdHTTP.Port:= StrToInt(URI.Port); IdHTTP.Request.UserAgent:= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'; IdHTTP.HandleRedirects:= True; IdHTTP.OnWork:= OnWorkHandler; IdHTTP.Get(TargetURI, pageStream); except //* Allow application to show current site if Assigned(Settings.pReportCurrentURI) then Settings.ReportCurrentURI(TargetURI + ' - third try '); try // --> Third try IdHTTP.Free; IdHTTP:= nil; IdHTTP:= TIdHTTP.Create(nil); IdHTTP.ReadTimeout:= intReadTimeout; if URI.Port <> '' then IdHTTP.Port:= StrToInt(URI.Port); IdHTTP.Request.UserAgent:= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'; IdHTTP.HandleRedirects:= True; IdHTTP.OnWork:= OnWorkHandler; IdHTTP.Get(TargetURI, pageStream); except on EIdReadTimeout do begin noErrors:= False; Settings.Log('UPage. IdHTTP.ReadTimeout: ' + TargetURI, True); end else begin noErrors:= False; Settings.Log('UPage. IdHTTP: ''' + IdHTTP.ResponseText + ''': ' + TargetURI, True); end; end; // <-- Third try end; // <-- Second tryend; // <-- First try//**********//* Start the parsingif noErrors then ParsePage;
function Tverpipxinfo.postXml(const xmlstr, url: WideString): WideString;
var
idHttp:TIdHTTP;
sends:tstrings;
IdEncoderMIME1:TIdEncoderMIME;
begin
result:='';
try
idHttp:= TIdHTTP.Create(nil);
idHttp.Request.ContentType := 'application/x-www-form-urlencoded';
IdEncoderMIME1:=TIdEncoderMIME.Create(nil);
sends:=tstringlist.Create;
sends.Add('xmlstr='+IdEncoderMIME1.Encode(xmlstr));
result:=idhttp.Post(url,sends);
except
on E:Exception do
begin
result:=e.Message;
end;
end;
idHttp.Free;
IdEncoderMIME1.Free;
sends.Free;
end;asp端接收方法
<%@ Language=VBScript %>
<%'On Error Resume Next
xmlstr=Request.form("xmlstr")
set xmlobj=server.CreateObject("microsoft.xmldom")
xmlobj.loadXML xmlstr
Response.ContentType="text/xml"%>
<?xml version="1.0" encoding="big5"?>
<%
Response.Write xmlobj.xml
set xmlobj=nothing
%>aspx端接收方法
private void Page_Load(object sender, System.EventArgs e)
{
string str = "";
string reqstr="";
try
{
XmlDocument doc = new XmlDocument();
//doc.Load(Request.InputStream);
reqstr=Request.Form["xmlstr"];
reqstr =Encoding.GetEncoding("big5").GetString(Convert.FromBase64String(reqstr));
doc.LoadXml(reqstr);
doc.Save("d:/test.xml");
Response.Write("How are you..");
}
catch(Exception e1)
{
str = e1.Message;
}
Response.Write("str ==" + str);
}
上面只是简单的用GET方法,如果你访问的网页需要提交大量数据,比如登入网站的用户名、密码等信息可以使用POST的方法来提交表单。即将提交的信息加载到一个列表中,然后作为参数传送。
使用TIdHTTP控件可以实现上传下载文件功能。
错误信息:
1. HTTP/1.1 400 Bad Request
说明:请求出现语法错误
解决方法:检测请求的网址是否正确,注意网址要写网整的,如:http://www.163.com/而不是www.163.com
2. HTTP/1.1 404 Object Not Found或HTTP/1.1 405 Method not allowed
说明:网络问题…… 我出现问题是IIS的设置与访问的站点冲突。
3. Socket Error # 10061 Connection refused
说明:10061的错误说明:由于以前的关闭调用,套接字在那个方向已经关闭,发送或接收数据的请求没有被接受。估计是你在程序中已经关闭多次.
解决方法:重新启动电脑,解决此问题。
下面是简单的介绍:
首先在Form上放idHttp, IdCookieManager,并将两者联系在一起
(通过idHttp的CookieManager),剩下的 你就专心实现你要完成
的东西就可以啦。
不知道你玩过KOK没有,下面的代码是登陆KOK注册页面的部分代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, IdCookieManager, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;
type
TForm1 = class(TForm)
http: TIdHTTP;
CookieMngr: TIdCookieManager;
edtUserName: TLabeledEdit;
edtPassword: TLabeledEdit;
btnLogin: TButton;
Cookies: TMemo;
Memo1: TMemo;
btnInfor: TButton;
Button1: TButton;
edtSN: TLabeledEdit;
procedure btnLoginClick(Sender: TObject);
procedure btnInforClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//这个函数是登陆页面,
procedure TForm1.btnLoginClick(Sender: TObject);
var
s, s1: TStringStream;
i: Integer;
begin
s := TStringStream.Create('');
s1 := TStringStream.Create('');
try
//{
s.WriteString('action=LOGIN&gameSelect=gkk&');
s.WriteString('acct=' + edtUserName.Text + '&');
s.WriteString('pwd=' + edtPassword.Text);
http.Request.ContentType := 'application/x-www-form-urlencoded';
try
http.Post('http://register.kok.com.cn/billing/servlet/walletServlet', s, s1)
except
http.Get(http.Response.Location, s1);
end;
//}
Memo1.Lines.Text := s1.DataString;
//下面的是显示cookies信息的代码
Cookies.Clear;
for i := 0 to CookieMngr.CookieCollection.Count - 1 do
Cookies.Lines.Add(CookieMngr.CookieCollection.Items.CookieText);
finally
s.Free;
s1.Free;
end;
end;
//这是KOK注册页面中,显示帐户信息的函数
procedure TForm1.btnInforClick(Sender: TObject);
var
s, s1: TStringStream;
i: Integer;
begin
{
<form action=../servlet/walletServlet method=post>
<input type=submit value="修改个人资料">
<input type=hidden name=action value=INFO>
</form>
}
s := TStringStream.Create('');
s1 := TStringStream.Create('');
try
s.WriteString('action=INFO');
http.Request.ContentType := 'application/x-www-form-urlencoded';
try
http.Post('http://register.kok.com.cn/billing/servlet/walletServlet', s, s1)
except
http.Get(http.Response.Location, s1);
end;
Memo1.Lines.Text := s1.DataString;
Cookies.Clear;
for i := 0 to CookieMngr.CookieCollection.Count - 1 do
Cookies.Lines.Add(CookieMngr.CookieCollection.Items.CookieText);
finally
s.Free;
s1.Free;
end;
end;
end.
来自:yannqi, 时间:2003-12-22 14:05:00, ID:2369046
to:andy263
我按照你的代码访问我的一个asp网页好像不行!
KOK我没有玩过,能否给个登录某个asp的cookie的论坛的例子,如asp动网先锋的论坛。
谢谢!
来自:andy263, 时间:2003-12-22 14:09:00, ID:2369062
procedure TForm1.Button1Click(Sender: TObject);
const
DFW_LOGIN_URL = 'http://www.delphibbs.com/delphibbs/chkuser.asp';
UserName = '你的用户名';
Password = '你的密码'; //晕,刚才把密码写出来了,得改一下了
var
Params: TStrings;
HTML: String;
begin
Params:=TStringList.Create;
try
Params.Add('URL='+'/delphibbs/collections.asp');
//登录成功后跳转到的URL,这里直接转到"我收藏的问题"
Params.Add('QUERY_STRING='); //登录成功后跳转URL的参数
Params.Add('txtName='+UserName);
Params.Add('txtPass='+Password);
//Params.Add('chkSave='); //是否记住我的密码
IdHttp.HandleRedirects:=True;
HTML:=IdHttp.Post(DFW_LOGIN_URL,Params);
if Pos('<USER Name="" />',HTML)>0 then
ShowMessage('登录失败!') //登录失败则用户名为空
else
ShowMessage('登录成功!');
//分析HTML,取出每一条收藏贴子的URL,下载保存
finally
Params.Free;
end;
end;