Http request test

Source: Internet
Author: User

Import java. io. bufferedReader; import java. io. IOException; import java. io. inputStream; import java. io. inputStreamReader; import java.net. httpURLConnection; import java.net. URL; import java.net. URLEncoder; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. map. entry; public class HttpRequestProxy {// connection timeout private static int connectTimeOut = 5000; // read data timeout private static I Nt readTimeOut = 10000; // Request Encoding private static String requestEncoding = "UTF-8 "; /*** <pre> * Send an HTTP request with parameters * </pre> ** @ param reqUrl * HTTP request URL * @ param parameters * parameter ing table *@ return HTTP Response String */public static String doGet (String reqUrl, map parameters) {HttpURLConnection url_con = null; String responseContent = null; try {StringBuffer params = new StringBuffer (); for (Iterator iter = parameters. e NtrySet (). iterator (); iter. hasNext ();) {Entry element = (Entry) iter. next (); params. append (element. getKey (). toString (); params. append ("="); params. append (URLEncoder. encode (element. getValue (). toString (), HttpRequestProxy. requestEncoding); params. append ("&");} if (params. length ()> 0) {params = params. deleteCharAt (params. length ()-1);} URL url = new URL (reqUrl); url_con = (HttpURLConnection) url. openCon Nection (); url_con.setRequestMethod ("GET"); // System. setProperty ("sun.net. client. defaulttimetimeout ", String. valueOf (HttpRequestProxy. connectTimeOut); // (unit: milliseconds) Change jdk1.4 to this, connection timeout // System. setProperty ("sun.net. client. defaultReadTimeout ", String. valueOf (HttpRequestProxy. readTimeOut); // (unit: milliseconds) replace jdk1.4 with this. The read operation times out url_con.setConnectTimeout (getConnectTimeOut (); // (unit: milliseconds) jdk 1.5 with this, and the connection times out url_con.setReadTim Eout (getReadTimeOut (); // (unit: milliseconds) jdk 1.5 is replaced with this, and the read operation times out url_con.setDoOutput (true); byte [] B = params. toString (). getBytes (); url_con.getOutputStream (). write (B, 0, B. length); url_con.getOutputStream (). flush (); url_con.getOutputStream (). close (); InputStream in = url_con.getInputStream (); BufferedReader rd = new BufferedReader (new InputStreamReader (in, getRequestEncoding (); String tempLine = rd. readLine (); StringB Uffer temp = new StringBuffer (); String crlf = System. getProperty ("line. separator"); while (tempLine! = Null) {temp. append (tempLine); temp. append (crlf); tempLine = rd. readLine ();} responseContent = temp. toString (); rd. close (); in. close ();} catch (IOException e) {System. err. println ("network fault"); e. printStackTrace ();} finally {if (url_con! = Null) {url_con.disconnect () ;}} return responseContent ;} /*** <pre> * Send HTTP requests without parameters * </pre> ** @ param reqUrl * HTTP request URL * @ return HTTP Response string */ public static String doGet (String reqUrl) {HttpURLConnection url_con = null; String responseContent = null; try {StringBuffer params = new StringBuffer (); String queryUrl = reqUrl; URL url = new URL (queryUrl); url_con = (HttpURLConnection) url. openConnecti On (); url_con.setRequestMethod ("GET"); // System. setProperty ("sun.net. client. defaulttimetimeout ", String. valueOf (HttpRequestProxy. connectTimeOut); // (unit: milliseconds) Change jdk1.4 to this, connection timeout // System. setProperty ("sun.net. client. defaultReadTimeout ", String. valueOf (HttpRequestProxy. readTimeOut); // (unit: milliseconds) Change jdk1.4 to this, and the read operation times out url_con.setConnectTimeout (getConnectTimeOut (); // (unit: milliseconds) Change jdk 1.5 to this, connection timeout url_con.setReadTimeout (GetReadTimeOut (); // (unit: milliseconds) jdk 1.5 is replaced with this, and the read operation times out url_con.setDoOutput (true); byte [] B = params. toString (). getBytes (); url_con.getOutputStream (). write (B, 0, B. length); url_con.getOutputStream (). flush (); url_con.getOutputStream (). close (); InputStream in = url_con.getInputStream (); BufferedReader rd = new BufferedReader (new InputStreamReader (in, getRequestEncoding (); String tempLine = rd. readLine (); StringBuffer Temp = new StringBuffer (); String crlf = System. getProperty ("line. separator"); while (tempLine! = Null) {temp. append (tempLine); temp. append (crlf); tempLine = rd. readLine ();} responseContent = temp. toString (); rd. close (); in. close ();} catch (IOException e) {System. err. println ("network fault"); e. printStackTrace ();} finally {if (url_con! = Null) {url_con.disconnect () ;}} return responseContent ;} /*** <pre> * Send an HTTP request with parameters for POST * </pre> ** @ param reqUrl * HTTP request URL * @ param parameters * parameter ing table *@ return HTTP Response String */public static String doPost (String reqUrl, map parameters, String verifyString) {HttpURLConnection url_con = null; String responseContent = null; try {StringBuffer params = new StringBuffer (); for (Iterator iter = parameters. e NtrySet (). iterator (); iter. hasNext ();) {Entry element = (Entry) iter. next (); params. append (element. getKey (). toString (); params. append ("="); params. append (URLEncoder. encode (element. getValue (). toString (), HttpRequestProxy. requestEncoding); params. append ("&");} if (params. length ()> 0) {params = params. deleteCharAt (params. length ()-1);} URL url = new URL (reqUrl); url_con = (HttpURLConnection) url. openConn Ection (); url_con.setRequestProperty ("Authorization", verifyString); url_con.setRequestMethod ("POST"); // System. setProperty ("sun.net. client. defaulttimetimeout ", String. valueOf (HttpRequestProxy. connectTimeOut); // (unit: milliseconds) Change jdk1.4 to this, connection timeout // System. setProperty ("sun.net. client. defaultReadTimeout ", String. valueOf (HttpRequestProxy. readTimeOut); // (unit: milliseconds) Change jdk1.4 to this, and the read operation times out url_con.setConnectTimeout (getCo NnectTimeOut (); // (unit: milliseconds) Change jdk1.5 to this, connection timeout url_con.setReadTimeout (getReadTimeOut (); // (unit: milliseconds) jdk 1.5 to this, read operation timeout url_con.setDoOutput (true); byte [] B = params. toString (). getBytes (); url_con.getOutputStream (). write (B, 0, B. length); url_con.getOutputStream (). flush (); url_con.getOutputStream (). close (); InputStream in = url_con.getInputStream (); BufferedReader rd = new BufferedReader (new InputStreamReader (in, ge TRequestEncoding (); String tempLine = rd. readLine (); StringBuffer tempStr = new StringBuffer (); String crlf = System. getProperty ("line. separator "); while (tempLine! = Null) {tempStr. append (tempLine); tempStr. append (crlf); tempLine = rd. readLine ();} responseContent = tempStr. toString (); rd. close (); in. close ();} catch (IOException e) {System. err. println ("network fault"); e. printStackTrace ();} finally {if (url_con! = Null) {url_con.disconnect () ;}return responseContent;} public static String doPost (String reqUrl, Map parameters) {HttpURLConnection url_con = null; String responseContent = null; try {StringBuffer params = new StringBuffer (); for (Iterator iter = parameters. entrySet (). iterator (); iter. hasNext ();) {Entry element = (Entry) iter. next (); params. append (element. getKey (). toString (); params. append ("="); param S. append (URLEncoder. encode (element. getValue (). toString (), HttpRequestProxy. requestEncoding); params. append ("&");} if (params. length ()> 0) {params = params. deleteCharAt (params. length ()-1);} URL url = new URL (reqUrl); url_con = (HttpURLConnection) url. openConnection (); url_con.setRequestMethod ("POST"); // System. setProperty ("sun.net. client. defaulttimetimeout ", String. valueOf (HttpRequestProxy. connec TTimeOut); // (unit: milliseconds) Change jdk1.4 to this, connection timeout // System. setProperty ("sun.net. client. defaultReadTimeout ", String. valueOf (HttpRequestProxy. readTimeOut); // (unit: milliseconds) Change jdk1.4 to this, and the read operation times out url_con.setConnectTimeout (getConnectTimeOut (); // (unit: milliseconds) Change jdk1.5 to this, connection timeout url_con.setReadTimeout (getConnectTimeOut (); // (unit: milliseconds) jdk 1.5 is replaced with this, read operation timeout url_con.setDoOutput (true); byte [] B = params. toString (). getBytes (); url_con.getOutputStream (). Write (B, 0, B. length); url_con.getOutputStream (). flush (); url_con.getOutputStream (). close (); InputStream in = url_con.getInputStream (); BufferedReader rd = new BufferedReader (new InputStreamReader (in, getRequestEncoding (); String tempLine = rd. readLine (); StringBuffer tempStr = new StringBuffer (); String crlf = System. getProperty ("line. separator "); while (tempLine! = Null) {tempStr. append (tempLine); tempStr. append (crlf); tempLine = rd. readLine ();} responseContent = tempStr. toString (); rd. close (); in. close ();} catch (IOException e) {System. err. println ("network fault"); e. printStackTrace ();} finally {if (url_con! = Null) {url_con.disconnect () ;}} return responseContent;}/*** @ return connection timeout (milliseconds) * @ see com. hengpeng. common. web. httpRequestProxy # connectTimeOut */public static int getConnectTimeOut () {return HttpRequestProxy. connectTimeOut;}/*** @ return timeout (milliseconds) * @ see com. hengpeng. common. web. httpRequestProxy # readTimeOut */public static int getReadTimeOut () {return HttpRequestProxy. readTimeOut;}/*** @ return Request Encoding * @ See com. hengpeng. common. web. httpRequestProxy # requestEncoding */public static String getRequestEncoding () {return requestEncoding;}/*** @ param connectTimeOut * connection timeout (MS) * @ see com. hengpeng. common. web. httpRequestProxy # connectTimeOut */public static void setConnectTimeOut (int connectTimeOut) {HttpRequestProxy. connectTimeOut = connectTimeOut;}/*** @ param readTimeOut * data read timeout (milliseconds) * @ see com. hengp Eng. common. web. httpRequestProxy # readTimeOut */public static void setReadTimeOut (int readTimeOut) {HttpRequestProxy. readTimeOut = readTimeOut;}/*** @ param requestEncoding * Request Encoding * @ see com. hengpeng. common. web. httpRequestProxy # requestEncoding */public static void setRequestEncoding (String requestEncoding) {HttpRequestProxy. requestEncoding = requestEncoding;} private static class Tester {public stati C void main (String [] args) {Map map = new HashMap (); map. put ("sxtid", "1109160001"); String temp = HttpRequestProxy. doGet ("http: // 61.147.68.123: 7001/GetVideoAddressHandler? Sxtid = 1109160001 ", map); System. out. println ("the returned message is:" + temp); temp = HttpRequestProxy. doGet ("http: // 61.147.68.123: 7001/GetVideoAddressHandler? Sxtid = 1109160001 "); System. out. println (" Returned message: "+ temp );}}}

 

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.