Custom HttpURLConnection tool class, httpurlconnection

Source: Internet
Author: User

Custom HttpURLConnection tool class, httpurlconnection

Public class CustomHttpURLConnection {private static String TAG = "CustomHttpUrlConnection"; private static HttpURLConnection conn; public CustomHttpURLConnection () {} public static String GetFromWebByHttpUrlConnection (String strUrl, NameValuePair... nameValuePairs) {String result = ""; try {URL url = new URL (strUrl); conn = (HttpURLConnection) url. openConnection (); conn. setDoInput (true); conn. setConnectTime Out (3000); conn. setreadtimeouts (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) {// TODO Auto-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 (); // sets whether to read data from httpUrlConnection. The default value is true; conn. setDoInput (true); // set whether to output data to httpUrlConnection. Because this is a post request, the parameter must be placed in the // http body. Therefore, it must be set to true, which is false by default; conn. setDoOutput (true); // set the request method to "POST". The default value is GET conn. setRequestMethod ("POS T "); // set timeout conn. setConnectTimeout (3000); conn. setReadTimeout (4000); // The cache conn cannot be used for Post requests. setUseCaches (false); conn. setInstanceFollowRedirects (true); // set that the transmitted content type is a serializable java object. // (If this option is not set, when the serialized object is transmitted, if the WEB service is not of this type by default, java may be thrown. io. EOFException) conn. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); // connection, from the above 2nd URLs. the current configuration of openConnection () must be completed before connect, // urlConn. connect (); InputStream in = c Onn. 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 ;}}}

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.