post傳參和擷取參數:
/** * 擷取post參數 * @param is * @param charset * @return */public static String getContent(InputStream is, String charset) {String pageString = null;InputStreamReader isr = null;BufferedReader br = null;StringBuffer sb = null;try {isr = new InputStreamReader(is, charset);br = new BufferedReader(isr);sb = new StringBuffer();String line = null;while ((line = br.readLine()) != null) {sb.append(line + "\n");}pageString = sb.toString();} catch (Exception e) {e.printStackTrace();} finally {try {if (is != null){is.close();}if(isr!=null){isr.close();}if(br!=null){br.close();}} catch (IOException e) {e.printStackTrace();}sb = null;}return pageString;}/** * @param url post傳參 * @param xml * @param method * @param contentType * @return */public static String xmlHttpProxy(String url,String xml,String method,String contentType){InputStream is = null;OutputStream os = null;/**介面調用成功標識*/boolean is_success = true;try {if(ValidateUtils.isEmpty(method)){method = "GET";}if(ValidateUtils.isEmpty(contentType)){contentType = "UTF-8";}URL _url = new URL(url);HttpURLConnection conn = (HttpURLConnection) _url.openConnection();conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-type", contentType); conn.setRequestMethod(method); //conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");//接收參數if(!ValidateUtils.isEmpty(xml)){os = conn.getOutputStream();os.write(xml.getBytes());os.flush();}//傳回值is = conn.getInputStream();return getContent(is, "utf-8");} catch (MalformedURLException e) {is_success = false;e.printStackTrace();} catch (IOException e) {is_success = false;e.printStackTrace();} finally{try {if(os!=null){os.close();}if(is!=null){is.close();}} catch (IOException e) {e.printStackTrace();}}return null;}
補充:
①post端:
/** * 是否同步成功 * 如果能夠ping通就表示同步成功,其他邏輯(比如是否真的成功了)在天翼客服完善 * syncType:1 insert;2 update;3 delete * @return */public boolean syncIsOk(String dataJson,int syncType){InputStreamReader in = null;BufferedReader reader = null;try{if(!ValidateUtils.isEmpty(dataJson)){String tianyikefu_ip = "http://172.172.3.134:8080";//拼上地址tianyikefu_ip = tianyikefu_ip + "/manyou/sync_roaming_data.jspx";System.out.println(tianyikefu_ip);String url = tianyikefu_ip;StringBuffer param_xml = new StringBuffer(200);param_xml.append("{\"dataJson\":"+dataJson+",\"syncType\":"+syncType+"}");//System.out.println(param_xml);String xml = HttpRequestUtils.xmlHttpProxy(url, param_xml.toString(), "POST","application/xml");//System.out.println("返回:" + xml);/** * { "resCode": 200, "resDesc": "處理成功!"} */JSONObject jsonObject = JSONObject.fromObject(xml);if(jsonObject!=null&&"200".equals(jsonObject.get("resCode").toString())){return true;}}}catch (Exception e) {e.printStackTrace();}finally{try{if(reader!=null){reader.close();}if(in!=null){in.close();}}catch (Exception e) {e.printStackTrace();}}return false;}
②擷取端:
StringBuffer info=new StringBuffer();in = request.getInputStream();buf = new BufferedInputStream(in);byte[] buffer=new byte[1024]; int iRead;while((iRead=buf.read(buffer))!=-1){info.append(new String(buffer,0,iRead,"gbk"));}if(info!=null&&!ValidateUtils.isEmpty(info.toString())){JSONObject paramJson = JSONObject.fromObject(info.toString());/* * {"dataJson":"","syncType":2} */if(paramJson!=null){dataJson = paramJson.getString("dataJson");syncType = paramJson.getString("syncType");}}