国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

jdk HttpURLConnection類請求http接口工具類

2019-11-06 06:19:07
字體:
來源:轉載
供稿:網友
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;public class HttpUtil {// post請求public static final String HTTP_POST = "POST";// get請求public static final String HTTP_GET = "GET";// utf-8字符編碼public static final String CHARSET_UTF_8 = "UTF-8";// HTTP內容類型public static final String CONTENT_TYPE_TEXT_HTML = "text/xml";// HTTP內容類型public static final String CONTENT_TYPE_FORM_URL = "application/x-www-form-urlencoded;charset=" + CHARSET_UTF_8;// 請求超時時間public static final int SEND_REQUEST_TIME_OUT = 20000;// 將讀超時時間public static final int READ_TIME_OUT = 20000;/*** * @param requestType*            請求類型* @param urlStr*            請求地址* @param body*            請求發送內容* @return 返回內容* @throws Exception */public static String requestMethod(String requestType, String urlStr, String body) throws Exception {//long star = System.currentTimeMillis();boolean isDoInput = false;if (body != null && body.length() > 0){isDoInput = true;}OutputStream outputStream = null;OutputStreamWriter outputStreamWriter = null;InputStream inputStream = null;InputStreamReader inputStreamReader = null;BufferedReader reader = null;StringBuffer resultBuffer = new StringBuffer();String tempLine = null;try {URL url = new URL(urlStr);URLConnection urlConnection = url.openConnection();HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;if (isDoInput) {httpURLConnection.setDoOutput(true);httpURLConnection.setRequestPRoperty("Content-Length", String.valueOf(body.length()));}httpURLConnection.setDoInput(true);httpURLConnection.setConnectTimeout(SEND_REQUEST_TIME_OUT);httpURLConnection.setReadTimeout(READ_TIME_OUT);httpURLConnection.setUseCaches(false);//httpURLConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");  httpURLConnection.setRequestProperty("Accept-Charset", CHARSET_UTF_8);httpURLConnection.setRequestProperty("Content-Type", CONTENT_TYPE_FORM_URL);httpURLConnection.setRequestMethod(requestType);httpURLConnection.connect();if (isDoInput) {outputStream = httpURLConnection.getOutputStream();outputStreamWriter = new OutputStreamWriter(outputStream);outputStreamWriter.write(body);outputStreamWriter.flush();// 刷新}if (httpURLConnection.getResponseCode() >= 300) {throw new Exception("http error code is :" + httpURLConnection.getResponseCode());}if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {inputStream = httpURLConnection.getInputStream();inputStreamReader = new InputStreamReader(inputStream, CHARSET_UTF_8);reader = new BufferedReader(inputStreamReader);while ((tempLine = reader.readLine()) != null) {resultBuffer.append(tempLine);}}//httpURLConnection.disconnect();} catch (MalformedURLException e) {e.printStackTrace();} finally {// 關閉流try {if (outputStreamWriter != null) {outputStreamWriter.close();}} catch (Exception e) {e.printStackTrace();}try {if (outputStream != null) {outputStream.close();}} catch (Exception e) {e.printStackTrace();}try {if (reader != null) {reader.close();}} catch (Exception e) {e.printStackTrace();}try {if (inputStreamReader != null) {inputStreamReader.close();}} catch (Exception e) {e.printStackTrace();}try {if (inputStream != null) {inputStream.close();}} catch (Exception e) {e.printStackTrace();}}//System.out.println("耗時:"+(System.currentTimeMillis()-star)+"毫秒");return resultBuffer.toString();}/*** 請求重試機制 二次請求需等待0.5秒* @param requestType 請求類型* @param action 請求接口* @param params 請求參數 json格式* @param retry true-重試, false-不重試* @return*/public static String retryHandler(String requestType, String action, String params, boolean retry){try {return requestMethod(requestType,action,params);} catch (Exception e) {e.printStackTrace();if(retry){try {Thread.sleep(500);return requestMethod(requestType,action,params);} catch (Exception e1) {e1.printStackTrace();}}}return "-100001"; //服務端接口出錯}public static void main(String[] args) {System.out.println("----------------star------------------");System.out.println(ApiInterface.CHANNEL_INFO);//String params = "{/"msgheader/":{/"route_id/":/"LZZBGE/",/"route_name/":/"1/",/"login_type/":/"1/",/"mobile/":/"1/",/"timestamp/":/"1/"},/"msgbody/":{/"nonce/":/"1/",/"sign/":/"7BBC156C8D9586E4B874385059FB966F/"}}";String params = "{/"msgheader/":{},/"msgbody/":{/"keyWord/":/"/",/"sign/":/"DAA6320AE7D6C11F369E80808E52A697/"}}";try {System.out.println(requestMethod(HTTP_POST,ApiInterface.PRODUCT_LIST,params));} catch (Exception e) {e.printStackTrace();System.out.println("Exception");}System.out.println("----------------end------------------");}}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石门县| 北碚区| 朝阳区| 天气| 东乡族自治县| 红原县| 观塘区| 永城市| 聂拉木县| 灵丘县| 诏安县| 浠水县| 韶山市| 呈贡县| 胶南市| 汽车| 龙门县| 象州县| 佛冈县| 体育| 卢湾区| 林芝县| 北辰区| 平江县| 西华县| 历史| 富平县| 吴堡县| 汶川县| 筠连县| 聂荣县| 隆尧县| 革吉县| 阳新县| 正定县| 清徐县| 淮安市| 汤阴县| 黑龙江省| 延安市| 那曲县|