java "application/json" 3個例子,懶
阿新 • • 發佈:2019-02-10
/** * java.net * application/json * @param strURL * @param params * @return */ public static String jsonNetPost(String strURL, String params) { try { // 建立連線 URL url = new URL(strURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); // 設定請求方式 connection.setRequestMethod("POST"); // 設定接收資料的格式 connection.setRequestProperty("Accept", "application/json"); // 設定傳送資料的格式 connection.setRequestProperty("Content-Type", "application/json"); // utf-8編碼 connection.connect(); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); out.append(params); out.flush(); out.close(); // 讀取響應 // 獲取長度 int length = (int) connection.getContentLength(); InputStream is = connection.getInputStream(); if (length != -1) { byte[] data = new byte[length]; byte[] temp = new byte[512]; int readLen = 0; int destPos = 0; while ((readLen = is.read(temp)) > 0) { System.arraycopy(temp, 0, data, destPos, readLen); destPos += readLen; } String result = new String(data, "UTF-8"); // utf-8編碼 return result; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 自定義錯誤資訊 return "請求失敗"; } /** * httpclient3 jsonPost * @param strURL * @param map * @return */ public static String jsonPostC3(String strURL, Map<String,Object> map ) { String returnStr =""; try { PostMethod method = new PostMethod(strURL); ObjectMapper mapper = new ObjectMapper(); StringWriter sw = new StringWriter(); JsonGenerator gen = new JsonFactory().createJsonGenerator(sw); mapper.writeValue(gen, map); gen.close(); String dataStr = sw.toString(); byte[] b = dataStr.getBytes("utf-8"); InputStream is = new ByteArrayInputStream(b, 0, b.length); RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/json;charset=utf-8"); method.setRequestEntity(re); //System.out.println(dataStr); org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(); int state = httpClient.executeMethod(method); //System.out.println(state+"<-----state"); if(state == 200){ //System.out.println(method.getResponseBodyAsString()+"<-wxSendResult"); } //returnStr =method.getResponseBodyAsString(); byte[] data =method.getResponseBody(); // utf-8編碼 returnStr = new String(data, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return returnStr; } /** * httpclient4 jsonPost * @param url * @param params * @param charset * @param connectionTimeout * @param soTimeout * @return */ public static String jsonPost(String url, Map<String,Object> params,String charset,Integer connectionTimeout,Integer soTimeout) { HttpClient hc = new DefaultHttpClient(); String body = null; try { // Post請求 HttpPost httppost = new HttpPost(url); // 設定引數 if(connectionTimeout!=null){ HttpConnectionParams.setConnectionTimeout(httppost.getParams(), connectionTimeout); } if(soTimeout!=null){ HttpConnectionParams.setSoTimeout(httppost.getParams(), soTimeout); } StringEntity strEntity =new StringEntity(JSONObject.fromObject(params).toString(),charset ); strEntity.setContentEncoding(charset); strEntity.setContentType("application/json"); httppost.setEntity(strEntity); // 傳送請求 HttpResponse httpresponse = hc.execute(httppost); // 獲取返回資料 HttpEntity entity = httpresponse.getEntity(); //body = EntityUtils.toString(entity); byte[] data =EntityUtils.toByteArray(entity); // utf-8編碼 body = new String(data, charset); EntityUtils.consume(entity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { hc.getConnectionManager().shutdown(); } catch (Exception ignore) {} } return body; }