您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關怎么在Java中發送post,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
1、post請求用于發送json 格式的參數:
/** * post請求(用于請求json格式的參數) * * @param url 地址 * @param params json格式的參數 * @return */ public static String doPost(String url, String params) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost( url );// 創建httpPost httpPost.setHeader( "Accept", "application/json" ); httpPost.setHeader( "Content-Type", "application/json" ); String charSet = "UTF-8"; StringEntity entity = new StringEntity( params, charSet ); httpPost.setEntity( entity ); CloseableHttpResponse response = null; try { response = httpclient.execute( httpPost ); StatusLine status = response.getStatusLine(); int state = status.getStatusCode(); if (state == HttpStatus.SC_OK) { HttpEntity responseEntity = response.getEntity(); String jsonString = EntityUtils.toString( responseEntity ); return jsonString; } else { logger.error( "請求返回:" + state + "(" + url + ")" ); } } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
2、用于發送key-value格式的參數
/** * post請求(用于key-value格式的參數) * * @param url * @param params * @return */ public static String doPost(String url, Map params) { BufferedReader in = null; try { // 定義HttpClient HttpClient client = new DefaultHttpClient(); // 實例化HTTP方法 HttpPost request = new HttpPost(); request.setURI( new URI( url ) ); //設置參數 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for (Iterator iter = params.keySet().iterator(); iter.hasNext(); ) { String name = (String) iter.next(); String value = String.valueOf( params.get( name ) ); nvps.add( new BasicNameValuePair( name, value ) ); //System.out.println(name +"-"+value); } request.setEntity( new UrlEncodedFormEntity( nvps, HTTP.UTF_8 ) ); HttpResponse response = client.execute( request ); int code = response.getStatusLine().getStatusCode(); if (code == 200) { //請求成功 in = new BufferedReader( new InputStreamReader( response.getEntity() .getContent(), "utf-8" ) ); StringBuffer sb = new StringBuffer( "" ); String line = ""; String NL = System.getProperty( "line.separator" ); while ((line = in.readLine()) != null) { sb.append( line + NL ); } in.close(); return sb.toString(); } else { // System.out.println( "狀態碼:" + code ); return null; } } catch (Exception e) { e.printStackTrace(); return null; } }
第三,發送get請求
/** * get請求 * * @return */ public static String doGet(String url) { try { HttpClient client = new DefaultHttpClient(); //發送get請求 HttpGet request = new HttpGet( url ); HttpResponse response = client.execute( request ); /**請求發送成功,并得到響應**/ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { /**讀取服務器返回過來的json字符串數據**/ String strResult = EntityUtils.toString( response.getEntity() ); return strResult; } } catch (IOException e) { e.printStackTrace(); } return null; }
Java的特點有哪些 1.Java語言作為靜態面向對象編程語言的代表,實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。 2.Java具有簡單性、面向對象、分布式、安全性、平臺獨立與可移植性、動態性等特點。 3.使用Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
以上就是怎么在Java中發送post,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。