Post/get data in Delphi

Source: Internet
Author: User

Method 1: Use the TWebBrowser component
Unit Unit1;

Interface

Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw;

Type
TForm1 = class (TForm)
WebBrowser1: TWebBrowser;
Memo1: TMemo;
Button1: TButton;
Procedure Button1Click (Sender: TObject );
*******
{Private declarations}
Public
{Public declarations}
End;

Var
Form1: TForm1;

Implementation

{$ R *. dfm}

Procedure TForm1.Button1Click (Sender: TObject );
Var
URL: OleVariant;
POST: OleVariant;
Target: OleVariant;
Flag: OleVariant;
Header: OleVariant;
PostStr: String;
PostLen, Index: Integer;
Begin
URL: = 'HTTP: // port.fudan.edu.cn: 58080/amserver/UI/Login? Goto = http % 3A % 2F % 2Fwww.urp.fudan.edu.cn % 3A81% 2 Fepstar % 2fr % 2Fpr % 2 Fgwkh % 2 Fdzgwkh % 2Fgr_dzgwkh_index.jsp & gotoOnFail = http://www.urp.fudan.edu.cn/portal ?. Flag = fail ';
PostStr: = 'login. Token1 = 04538 & Login. Token2 = ******* & Submit = % B5 % C7 + % C2 % bc ';
Target: = NULL;
Flag: = 0;
PostLen: = Length (PostStr );
// Build a Variant array of the varByte type
Post: = VarArrayCreate ([0, (PostLen-1)], varByte );
// Fill in data
For Index: = 0 to PostLen-1 do
Post [Index]: = Ord (PostStr [Index + 1]);
// Fill the header data. Note: application
Header: = 'content-Type: application/x-www-form-urlencoded ';

WebBrowser1.Navigate2 (URL, flag, target, POST, Header );
While WebBrowser1.Busy = True do
Application. ProcessMessages;
If WebBrowser1.ReadyState <> READYSTATE_COMPLETE then
Memo1.Text: = WebBrowser1.OleObject. Document. all. tags ('html'). Item (0). outerHTML;
Else
Memo1.Text: = 'page loading failed ';
End;
End.

Method 2: use the XMLHTTP component use comobj, ActiveX,
Var
XmlHTTP: oleVariant;
Code0: OleVariant;
Begin
Code0: = CODE + '& DataGrid1 % 3A_ctl' + inttostr (strtoint (form1.Edit2. Text) + 1) + '% 3Azhj1 = On ';
While not connectfalg do
Begin
Try
CoInitialize (nil );
XmlHttp: = CreateOleObject ('msxml2. xmlhttp ');
Except
CoUninitialize ();
Continue;
End;

Try
Begin;
XmlHttp. open ('post', Url, false );
XmlHttp. setRequestHeader ('content-type', 'application/x-www-form-urlencoded ');
XmlHttp. send (code0 );
Htmlcode: = xmlHttp. responsetext;
If xmlHttp. status = '000000' then
Begin
Connectfalg: = True;
Result: = 0;
XmlHttp: = Unassigned;
CoUninitialize ();
Exit;
End;
End;
Except
On E: Exception do
Begin
ShowMessage (e. Message );
End;
End;
End;
Result: =-1;
End;

Release 3: TIdhttp component use IdHTTP
Function Getwebs (): Integer; stdcall; // thread Function
Var
Myhttp: tidhttp;
Param: string;
Begin
Myhttp: = tidhttp. Create (NiL );
Myhttp. readtimeout: = 30000;
Try
Begin
Cc: = myhttp. Get (PARAM );
End;
Except
Begin
Myhttp. Free;
Result: = 1;
Exit;
End;
End;
Result: = 0;
End;

Additional example: <wininet functions>

DWORD dwconnectstate;
Cstring strconnectstate;
Bool Bok = internetgetconnectedstate (& dwconnectstate, 0 );
If (Bok)
{
If (dwconnectstate & internet_connection_lan)
StrConnectState = "Local system uses a local area network to connect to the Internet .";
If (dwConnectState & INTERNET_CONNECTION_MODEM)
StrConnectState = "Local system uses a modem to connect to the Internet .";
If (dwConnectState & INTERNET_CONNECTION_MODEM_BUSY)
StrConnectState = "No longer used .";
If (dwConnectState = INTERNET_CONNECTION_PROXY)
StrConnectState = "Local system uses a proxy server to connect to the Internet .";
}
Test whether the connection is valid. You can use: InternetCheckConnection.

It is easier to check whether computers are connected to the Internet through a Win32 Internet (WinInet) function InternetCheckConnection;
This function is used to check whether an Internet connection can be established.
In % SystemRoot % \ System32 \ wininet. dll, the Delphi call declaration is in WinInet. pas,
Its API declaration is as follows:

BOOL InternetCheckConnection (
In lpcstr lpszUrl,
In dword dwFlags,
In dword dwReserved
);

The parameter indicates:

LpszUrl: the URL to be tested to establish a connection. It can be blank;
DwFlags: currently, it can only be FLAG_ICC_FORCE_CONNECTION (this constant is not declared in Delphi and its value is $00000001 );
DwReserved: currently, it can only be 0.

Call description:

If lpszUrl is not empty, Windows parses the Host name from it and then Ping the specified Host.
If lpszUrl is empty and the database of the WinInet internal Server has a record about the latest Server, Windows will resolve the Host name from this record and Ping it.

If a successful connection succeeds, True is returned; otherwise, False is returned;

The following is an example of determining whether a computer is connected to IOT platform:

Procedure TForm1.Button1Click (Sender: TObject );
Begin
If InternetCheckConnection ('HTTP: // www.yahoo.com/', 1, 0) then
Edit1.text: = 'connected'
Else
Edit1.text: = 'disconnected ';
End;

The above method can only detect whether the current computer is physically connected, that is, whether the network cable is connected, whether the network adapter can work smoothly, and whether it can implement Internet services, whether to connect to the ISP over the Internet.
In this case, you can use another Win32 Internet (WinInet) function InternetQueryOption to detect the problem;
This function is used to query the status and options of the specified Internet handle.
Its API declaration is as follows:

BOOL InternetQueryOption (
In hinternet hInternet,
In dword dwOption,
Out lpvoid lpBuffer,
In out lpdword lpdwBufferLength
);

The parameter indicates:

HInternet: the Internet handle of the query object (nil for global query ),
DwOption: The queried project;
LpBuffer: returned query results;
LpdwBufferLength: the length of the query result IN bytes (including IN and OUT );

Returns True if the query is successful; otherwise, returns False;

When you want to query the Internet connection status of the current computer, you can use the query item INTERNET_OPTION_CONNECTED_STATE,

The returned value of ConnectState may be the sum of one or more of the following values:

INTERNET_STATE_CONNECTED: $00000001 connection status;
INTERNET_STATE_DISCONNECTED: $00000002 non-connection status (corresponding to INTERNET_STATE_CONNECTED );
INTERNET_STATE_DISCONNECTED_BY_USER: $00000010 non-connection status requested by the user
INTERNET_STATE_IDLE: $00000100 connection status, and idle
INTERNET_STATE_BUSY: $00000200 connection status, responding to connection request

The following is an example of determining whether the current computer can obtain Internet services:

Function TForm1.CheckOffline: boolean;
Var
ConnectState: DWORD;
StateSize: DWORD;
Begin
ConnectState: = 0;
StateSize: = SizeOf (ConnectState );
Result: = false;
If InternetQueryOption (nil, INTERNET_OPTION_CONNECTED_STATE, @ ConnectState, StateSize) then
If (ConnectState and INTERNET_STATE_DISCONNECTED) <> 2 then result: = true;
End;

Procedure TForm1.Button1Click (Sender: TObject );
Begin
If CheckOffline then
Edit1.text: = 'connect To ISP'
Else
Edit1.text: = 'disconnect To ISP ';
End;

It must be noted that the detection result of the InternetQueryOption function can only indicate that the current Internet settings are available,
It does not mean that the computer will be able to access the Internet. For example, if the network cable is down and the network adapter suddenly breaks down, it cannot be detected,
Two functions must be used together to check whether the current computer can obtain Internet services.

The above programs are successfully debugged in Win2000 and Delphi5.0.

Note that WinInet should be added to uses.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.