Customizing the HttpURLConnection tool class

Source: Internet
Author: User
Tags getmessage

public class Customhttpurlconnection {private static String TAG = "Customhttpurlconnection";p rivate static HttpURLConnection conn;public customhttpurlconnection () {}public static string getfromwebbyhttpurlconnection (string Strurl,namevaluepair. Namevaluepairs) {String result= ""; try {URL url = new URL (strurl); conn = (httpurlconnection) URL.O Penconnection (); Conn.setdoinput (true); Conn.setconnecttimeout (+); conn.setreadtimeout (4000); Conn.setrequestproperty ("Accept", "*/*");//int Rescode=conn.getresponsecode (); Conn.connect (); InputStream stream= Conn.getinputstream (); InputStreamReader inreader=new InputStreamReader (stream); BufferedReader buffer=new BufferedReader (Inreader); String Strline=null;while ((Strline=buffer.readline ())!=null) {result+=strline;} Inreader.close (); Conn.disconnect (); return result;} catch (Malformedurlexception e) {//TODO auto-generated catch blocklog.e (TAG, "getfromwebbyhttpurlconnection:" + E.getmessage ()); E.printstacktrace (); return null;} catch (IOException e) {//TODOAuto-generated catch blocklog.e (TAG, "Getfromwebbyhttpurlconnection:" +e.getmessage ()); E.printstacktrace (); return null;}} public static string Postfromwebbyhttpurlconnection (String Strurl,namevaluepair ... namevaluepairs) {string result= ""; try {URL url = new URL (strurl), conn = (httpurlconnection) url.openconnection ();//settings are read from HttpURLConnection, true by default; Conn.setdoinput (TRUE);//Set whether to output to httpurlconnection, because this is a POST request, the parameters are placed in//HTTP body, so it needs to be set to True, by default is false; Conn.setdooutput (TRUE);//The method to set the request is "POST", the default is Get Conn.setrequestmethod ("post");//Set timeout Conn.setconnecttimeout (3000) ; conn.setreadtimeout (4000);//Post request cannot use cache Conn.setusecaches (false); Conn.setinstancefollowredirects (true);// Sets the content type of the transfer to be a serializable Java object//(if this item is not set, when the serialized object is transferred, the Web service may throw java.io.EOFException when the default is not this type) Conn.setrequestproperty (" Content-type "," application/x-www-form-urlencoded ");//connection, url.openconnection () from the above 2nd () the configuration must be done before connect,// Urlconn.connect (); InputStream in = Conn.getinputstream (); InputStreamReader instream=new InputstreAmreader (in); BufferedReader buffer=new BufferedReader (instream); String Strline=null;while ((Strline=buffer.readline ())!=null) {result+=strline;} return result;} catch (IOException ex) {LOG.E (TAG, "postfromwebbyhttpurlconnection:" + ex.getmessage ()); Ex.printstacktrace (); return null;}}}

Customizing the HttpURLConnection tool class

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.