HTTP request in crazy android handout (get and post)

Source: Internet
Author: User
Package COM. example. downloadthread; import Java. io. *; import java.net. *; import Java. util. *;/*** @ author yeeku. h. lee kongyeeku@163.com * @ version 1.0 */public class getpostutil {/*** send a get Method Request to a specified URL ** @ Param URL * send the request URL * @ Param Params * request parameter, the request parameters should be in the form of name1 = value1 & name2 = value2. * @ Return URL indicates the response of the Remote resource */public static string sendget (string URL, string Params) {string result = ""; bufferedreader in = NULL; try {string urlname = URL + "? "+ Params; Url realurl = new URL (urlname); // enable the URL Connection urlconnection conn = realurl. openconnection (); // set the common request attribute Conn. setrequestproperty ("accept", "*/*"); Conn. setrequestproperty ("connection", "keep-alive"); Conn. setrequestproperty ("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1)"); // establish the actual connection Conn. connect (); // obtain all response header fields Map <string, list <string> map = Conn. getheaderfields (); // traverses Some response header fields for (string key: map. keyset () {system. out. println (Key + "--->" + map. get (key);} // defines the bufferedreader input stream to read the URL response in = new bufferedreader (New inputstreamreader (Conn. getinputstream (), "gb2312"); string line; while (line = in. readline ())! = NULL) {result + = "\ n" + line;} catch (exception e) {system. Out. println ("an exception occurred when sending a GET request! "+ E); E. printstacktrace () ;}// use the Finally block to close the input stream finally {try {If (in! = NULL) {In. close () ;}} catch (ioexception ex) {ex. printstacktrace () ;}} return result;}/*** send a POST method request to a specified URL ** @ Param URL * The request URL * @ Param Params * request parameter, the request parameters should be in the form of name1 = value1 & name2 = value2. * @ Return URL indicates the response of the Remote resource */public static string sendpost (string URL, string Params) {printwriter out = NULL; bufferedreader in = NULL; string result = ""; try {URL realurl = new URL (URL); // enable the URL Connection urlconnection conn = realurl. openconnection (); // set the common request attribute Conn. setrequestproperty ("accept", "*/*"); Conn. setrequestproperty ("connection", "keep-alive"); Conn. setrequestproperty ("User-Agent", "Mozilla/4.0 (C Ompatible; MSIE 6.0; Windows NT 5.1; sv1) "); // The following two lines of conn must be set to send a POST request. setdooutput (true); Conn. setdoinput (true); // get the output stream of the urlconnection object out = new printwriter (Conn. getoutputstream (); // sends the request parameter out. print (Params); // flush the Buffer out of the output stream. flush (); // defines the bufferedreader input stream to read the URL response in = new bufferedreader (New inputstreamreader (Conn. getinputstream (); string line; while (line = in. readline ())! = NULL) {result + = "\ n" + line;} catch (exception e) {system. Out. println ("an exception occurred when sending a POST request! "+ E); E. printstacktrace () ;}// use finally blocks to close the output stream and input stream finally {try {If (OUT! = NULL) {out. Close () ;}if (in! = NULL) {in. Close () ;}catch (ioexception ex) {ex. printstacktrace () ;}} return result ;}}

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.