Delphi idhttp Control: Get/post Request

Source: Internet
Author: User

have been using idhttp recently, here are some code about GET, POST request Basic use method

One, GET request

1 procedure Getdemo; 2 var 3   idhttp:tidhttp; 4   url:string;//request address 5   Responsestream:tstringstream;//return information 6   responsestr:s Tring 7 begin 8   //Create Idhttp Control 9   idhttp: = Tidhttp.create (nil); The   //tstringstream object is used to save the response information   one by one Responsestream: = Tstringstream.create (");   try13     //Request address     Url: = ' http://dict.youdao.com/'; 15     TRY16       Idhttp.get (Url,responsestream);     except18 on       e:exception do19 begin20         showmessage (e.message );       end;22     end;23     //Get the information returned by the webpage     responsestr: = responsestream.datastring;25     // The presence of Chinese in the Web page requires UTF8 decoding of     responsestr: = Utf8decode (RESPONSESTR);   finally28 idhttp.free;29     responsestream.free;30   end;   The end;

If get needs to add request parameter, then add it directly after address, use & connection between parameters

such as: Http://dict.youdao.com? param1=1&param2=2

Second, Post request

 1 procedure Postdemo; 2 var 3 idhttp:tidhttp; 4 url:string;//request address 5 responsestream:tstringstream; return information 6 responsestr:string;     7 8 requestlist:tstringlist; Request Information 9 requeststream:tstringstream;10 begin11//Create Idhttp control Idhttp: = Tidhttp.create (nil);//tstringstream The image is used to save the response information Responsestream: = Tstringstream.create ("); Requeststream: = Tstringstream.create ("); Requestlis T: = tstringlist.create;18 try19 Url: = ' http://f.youdao.com/?path=fanyi&vendor=fanyiinput '; Try21/ /submit parameters in a list of Requestlist.add (' Text=love '), Idhttp.post (Url,requestlist,responsestream), 24 25//Stream       Cross-requeststream.writestring (' text=love '); Idhttp.post (url,requeststream,responsestream); except29     On E:exception do30 begin31 showmessage (e.message); end;33 end;34//Get information returned from a webpage 35 RESPONSESTR: = responsestream.datastring;36//Web page in the presence of Chinese, the need for UTF8 decoding the PNS responsesTR: = Utf8decode (RESPONSESTR), finally39 idhttp.free;40 requestlist.free;41 requeststream.free;42 Resp onsestream.free;43 end;44 end;

The POST request uses the list form to submit the parameter more in the Web page.

However, in some APIs, the request format for post is in JSON format or XML, it is important to note that before initiating a request, you need to set the ContentType property and use the Stream method to submit

The above code is an example:

Submit JSON Format: IdHttp.Request.ContentType: = ' Application/json ';

Submission XML format: IdHttp.Request.ContentType: = ' text/xml ';

If not submitted in the required format, HTTP 1.1/415 is typically returned

Delphi idhttp Control: Get/post Request

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.