Basic usage of Idhttp

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>first, the basic usage of idhttp</strong></p></p><p><p></p></p><p><p>Idhttp and webbrowser, can realize the ability to crawl the remote Web page, but the HTTP way faster, more resource-saving, The disadvantage is the need to manually maintain cook, connection, etc.</p></p><p><p>The creation of Idhttp requires the introduction of Idhttp</p></p><p><p>Procedure Inithttp ();<br>Begin<br>http: = Tidhttp.create (nil);<br>http. Readtimeout: = 30000;<br>http. Onredirect: = onredirect;<br>http. request.accept: = ' image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */* ';<br>http. request.acceptlanguage: = ' ZH-CN ';<br>http. request.contenttype: = ' application/x-www-form-urlencoded ';<br>http. request.useragent: = ' mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon;. NET CLR 1.1.4322) ';</p></p><p><p>http. Proxyparams.proxyserver: = ' Proxy server address ';<br>http. proxyparams.proxyport: = ' Proxy server port ';<br>End</p></p><p><p></p></p><p><p><strong>second, How to obtain the cookie information returned by the server and add it to the request object of HTTP</strong></p></p><p><p></p></p><p>Procedure setcookie;<br>Var<br>i:integer;<br>tmp, cookie:string;<br>Begin<br>Cookie: = ';<br>For I: = 0 to Http. Response.rawheaders.count-1 do<br>Begin<br>Tmp: = Http. response.rawheaders[i];<br>If POS (' set-cookie: ', Lowercase (tmp)) = 0 Then Continue;<br>tmp: = Trim (Copy (tmp, Pos (' set-cookie: ', tmp) + length (' set-cookie: '), length (tmp)));<br>tmp: = Trim (Copy (tmp, 0, Pos ('; ', tmp)-1));<br>If cookie = ' then cookie: = tmp Else Cookie: = cookie + '; ' + tmp;<br>End<br>If Cookie <> ' then<br>Begin<br>For I: = 0 to Http. Request.rawheaders.count-1 do<br>Begin<br>Tmp: = Http. request.rawheaders[i];<br>If Pos (' cookie ', Lowercase (tmp)) = 0 Then Continue;<br>http. Request.RawHeaders.Delete (i);<br>break;<br>End<br>http. Request.RawHeaders.Add (' cookie: ' + cookie);<br>End<br>End</p><p><p></p></p><p><p><strong>third, How to get all the links in the Web page, make changes to the code you can also implement to find all the pictures and so on</strong></p></p><p><p></p></p><p><p>function Geturllist (data:string): tstringlist;<br>Var<br>i:integer;<br>list:tstringlist;<br>tmp:string;</p></p><p><p></p></p><p><p>function Split (Data, node:string): tstringlist;<br>Var<br>Count, i, j:integer;<br></p></p><p>function GetFieldCount (Data, node:string): Integer;<br>Var<br>i:integer;<br>Begin<br>Result: =-1;<br>I: = Pos (Node, Data);<br>If i = 0 Then Exit;<br>Result: = 0;<br>While I <> 0 do<br>Begin<br>INC (Result);<br>Delete (Data, 1, i + Length (Node)-1);<br>I: = Pos (Node, Data);<br>End<br>End<br>Begin<br>Result: = tstringlist.create;<br>Count: = GetFieldCount (Data, Node);<br>For I: = 0 to Count-1 do<br>Begin<br>J: = Pos (Node, Data);<br>Result.add (Copy (Data, 1, j-1));<br>Delete (Data, 1, j + Length (Node)-1);<br>End<br>Result.add (Data);<br>End<br>Begin<br>Result: = tstringlist.create;<br>Try<br>List: = Split (Data, ' href= ');<br>For I: = 1 to List.count-1 do<br>Begin<br>Tmp: = list[i];<br>tmp: = Copy (tmp, 0, Pos (' </a> ', tmp)-1);<br>tmp: = Copy (tmp, 0, Pos (' > ', tmp)-1);<br>If Pos (", tmp) <> 0 Then</p><p>tmp: = Copy (tmp, 0, Pos (", tmp)-1);<br>tmp: = q_replacestr (tmp, Char (34), ');<br>tmp: = q_replacestr (tmp, Char (39), ');<br>If not Compare (CI. Key, Tmp) then Continue;<br>If Copy (tmp, 1, 7) <> '/http ' Then<br>Begin<br>If Copy (tmp, 1, 1) = '. ' then tmp: = StringReplace (tmp, '. ', ', []);<br>If Copy (tmp, 1, 1) = '. ' then tmp: = StringReplace (tmp, '. ', ', []);<br>Try<br>Tmp: = '/http ' + http. Url. Host + ': ' + http. Url. Port + HTTP. Url. Path + tmp;<br>Except<br>End<br>End<br>If Result.indexof (tmp) <>-1 then Continue;<br>Result.add (tmp);<br>End<br>Freeandnil (List);<br>Except</p><p><p>End<br>End</p></p><p><p></p></p><p><p><strong>Iv. How to simulate the get method of HTTP to open a Web page</strong></p></p><p><p></p></p><p><p>function GetMethod (http:tidhttp; url:string; Max:integer): String;<br>Var<br>respdata:tstringstream;<br>Begin<br>Respdata: = Tstringstream.create (");<br>Try<br>Try<br>Http.get (URL, respdata);<br>Http.Request.Referer: = URL;<br>Result: = respdata.datastring;<br>Except<br>Dec (Max);<br>If Max = 0 Then<br>Begin<br>Result: = ';<br>Exit;<br>End<br>Result: = GetMethod (http, URL, Max);<br>End<br>Finally<br>Freeandnil (respdata);<br>End<br>End</p></p><p><p></p></p><p><p><strong>V. How to simulate the post method of HTTP submit a Web page</strong></p></p><p><p></p></p><p>function Postmethod (URL, data:string; max:integer): String;<br>Var<br>postdata, respdata:tstringstream;<br>Begin<br>Respdata: = Tstringstream.create (");<br>Postdata: = Tstringstream.create (Data);<br>Try<br>Try<br>If http = nil then Exit;<br>Http.post (URL, postdata, respdata);<br>Result: = respdata.datastring;<br>http. Request.referer: = URL;<br>Except<br>Dec (Max);<br>If Max = 0 Then<br>Begin<br>Result: = ';<br>Exit;<br>End<br>Result: = Postmethod (URL, Data, Max);<br>End<br>Finally<br>http. Disconnect;<br>Freeandnil (respdata);<br>Freeandnil (postdata);<br>End<br>End</p><p><p></p></p><p><p><strong>Vi. Falsification of Session</strong></p></p><p><p>Var<br>my_cookie,tmpcookie:string;</p></p><p><p>Begin<br>Aidhttp.get (' http://www.huochepiao.net/');<br>tmpcookie:=aidhttp.request.customheaders.values[' Set-cookie '];<br>If Pos ('; ', tmpcookie) >0 Then<br>My_cookie:=leftbstr (tmpcookie,pos ('; ', tmpcookie)-1)<br>Else<br>my_cookie:= tmpcookie;<br>//<br>aIdHTTP.Request.CustomHeaders.Clear;<br>AIdHTTP.Request.CustomHeaders.Add (' Cookie: ' +my_cookie ');</p></p><p><p>End</p></p><p><p>Basic usage of idhttp (rpm)</p></p></span>

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.