Wemall Doraemon the Android App Store system sends a request code for a GET method to a specified URL

Source: Internet
Author: User
Tags finally block

The OpenConnection () method of the URL returns a URLConnection object that represents the communication link between the application and the URL. The program can send requests to the URL through the URLConnection instance and read the resource referenced by the URL.

Typically creating a connection to a URL and sending a request, reading the resource referenced by this URL requires the following steps:
(1) Create a URLConnection object by calling the URL object OpenConnection () method.
(2) Set the parameters of the URLConnection and the normal request properties.
(3) If you just send a Get method request, use the Connect method to establish an actual connection between the remote resources, and if you need to send a post-mode request, you need to get the output stream for the URLConnection instance to send the request parameters.
(4) The remote resource becomes available, the program can access the remote resource's header field, or read the remote resource's data through the input stream.

Package Cn.edu.zzu.wemall.net;import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.io.printwriter;import java.net.url;import java.net.urlconnection;import Java.util.list;import java.util.Map;     Import Cn.edu.zzu.wemall.config.myconfig;public class HttpRequest {/** * a request to send a GET method to a specified URL * * @param URL     * Send request URL * @param param * Request parameter, request parameter should be name1=value1&name2=value2 form. * @return The response result of the remote resource represented by the URL * @throws Exception * * Modify by Liudewei */@SuppressWarnings ("unused") pu        Blic static string Sendgetwithparameter (string url, string param) throws Exception {string result = "";        BufferedReader in = null;            try {String urlnamestring = URL + "?" + param;            URL realurl = new URL (urlnamestring);            The connection between open and URL urlconnection connection = Realurl.openconnection (); Set the generic Request property Connection.setrequestproperty ("User-agent",                   Myconfig.clientuseragent);            Connection.setconnecttimeout (5000);            Establish the actual connection connection.connect ();            Get all response header fields Map<string, list<string>> Map = Connection.getheaderfields (); Traverse all response header fields for (String Key:map.keySet ()) {//System.out.println (key + "--->" + map.get (k EY)); Close response Header Output}//define BufferedReader input stream to read the response of the URL in = new BufferedReader (New Inputstreamread            ER (Connection.getinputstream ()));            String Line;            while (line = In.readline ()) = null) {result + = line;        }} catch (Exception e) {throw e; }//Use finally block to close the input stream finally {try {if (in! = null) {In.clos                E ();            }} catch (Exception E2) {throw e2;    }} return result; }               @SuppressWarnings ("unused") public static string Sendget (string url) throws Exception {string result        = "";        BufferedReader in = null;            try {URL realurl = new URL (URL);            The connection between open and URL urlconnection connection = Realurl.openconnection ();            Set the generic request attribute Connection.setrequestproperty ("accept", "*/*");            Connection.setrequestproperty ("Connection", "keep-alive");            Connection.setrequestproperty ("User-agent", myconfig.clientuseragent);            Connection.setconnecttimeout (5000);            Establish the actual connection connection.connect ();            Get all response header fields Map<string, list<string>> Map = Connection.getheaderfields (); Traverse all response header fields for (String Key:map.keySet ()) {//System.out.println (key + "--->" + map.get (k            EY)); }//define BufferedReader input stream to read the response of the URL in = new BufFeredreader (New InputStreamReader (Connection.getinputstream ()));            String Line;            while (line = In.readline ()) = null) {result + = line;        }} catch (Exception e) {throw e; }//Use finally block to close the input stream finally {try {if (in! = null) {In.clos                E ();            }} catch (Exception E2) {throw e2;    }} return result;            /** * Request to send a POST method to the specified URL * * @param URL * Send the requested URL * @param param *     Request parameters, request parameters should be in the form of name1=value1&name2=value2. * @return response results for remote resources * @throws Exception */public static string Sendpost (string url, string param) throws E        xception {printwriter out = null;        BufferedReader in = null;        String result = "";            try {URL realurl = new URL (URL); The connection between open and URL UrlcoNnection conn = Realurl.openconnection ();            Set the generic request attribute Conn.setrequestproperty ("accept", "*/*");            Conn.setrequestproperty ("Connection", "keep-alive");            Conn.setrequestproperty ("User-agent", myconfig.clientuseragent);            Conn.setconnecttimeout (5000);            The Send POST request must be set to the following two lines conn.setdooutput (true);            Conn.setdoinput (TRUE);            Gets the output stream corresponding to the URLConnection object out = new PrintWriter (Conn.getoutputstream ());            Send request parameter out.print (param);            Flush output Stream Buffer Out.flush (); Defines the BufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader (conn.getinput            Stream ()));            String Line;            while (line = In.readline ()) = null) {result + = line;        }} catch (Exception e) {throw e;   }//Use the finally block to close the output stream, input stream finally{try{             if (out!=null) {out.close ();                } if (In!=null) {in.close ();            }} catch (Exception ex) {throw ex;    }} return result; }    }

  

Original Details Address: http://git.oschina.net/zzunet/wemall-doraemon/commit/e8f303df5663dc69fe47bb9623222149d40e3956

Wemall doraemonandroid App Store details address: http://www.koahub.com/home/product/55

Wemall website Address: http://www.wemallshop.com

Wemall Open Source Micro-mall, mall, mall source code, level three distribution, micro-fresh, micro-fruit, micro-takeaway, micro-ordering---professional system

Wemall Doraemon the Android App Store system sends a request code for a GET method to a specified URL

Related Article

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.