post实现get post传参

post传参和获取参数:

 

[java] 
 
  1. /** 
  2.      * 获取post参数 
  3.      * @param is 
  4.      * @param charset 
  5.      * @return 
  6.      */  
  7.     public static String getContent(InputStream is, String charset) {  
  8.         String pageString = null;  
  9.         InputStreamReader isr = null;  
  10.         BufferedReader br = null;  
  11.         StringBuffer sb = null;  
  12.         try {  
  13.             isr = new InputStreamReader(is, charset);  
  14.             br = new BufferedReader(isr);  
  15.             sb = new StringBuffer();  
  16.             String line = null;  
  17.             while ((line = br.readLine()) != null) {  
  18.                 sb.append(line + "\n");  
  19.             }  
  20.             pageString = sb.toString();  
  21.         } catch (Exception e) {  
  22.             e.printStackTrace();  
  23.         } finally {  
  24.             try {  
  25.                 if (is != null){  
  26.                     is.close();  
  27.                 }  
  28.                 if(isr!=null){  
  29.                     isr.close();  
  30.                 }  
  31.                 if(br!=null){  
  32.                     br.close();  
  33.                 }  
  34.             } catch (IOException e) {  
  35.                 e.printStackTrace();  
  36.             }  
  37.             sb = null;  
  38.         }  
  39.         return pageString;  
  40.     }  
  41.       
  42.     /** 
  43.      * @param url post传参 
  44.      * @param xml 
  45.      * @param method 
  46.      * @param contentType 
  47.      * @return 
  48.      */  
  49.     public static String xmlHttpProxy(String url,String xml,String method,String contentType){  
  50.         InputStream is = null;  
  51.         OutputStream os = null;  
  52.         /**接口调用成功标识*/  
  53.         boolean is_success = true;  
  54.         try {  
  55.             if(ValidateUtils.isEmpty(method)){  
  56.                 method = "GET";  
  57.             }  
  58.             if(ValidateUtils.isEmpty(contentType)){  
  59.                 contentType = "UTF-8";  
  60.             }  
  61.             URL _url = new URL(url);  
  62.             HttpURLConnection conn = (HttpURLConnection) _url.openConnection();  
  63.             conn.setDoInput(true);     
  64.             conn.setDoOutput(true);     
  65.             conn.setRequestProperty("Content-type", contentType);  
  66.             conn.setRequestMethod(method);  
  67.             //conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");  
  68.             //接收参数  
  69.             if(!ValidateUtils.isEmpty(xml)){  
  70.                 os = conn.getOutputStream();  
  71.                 os.write(xml.getBytes());  
  72.                 os.flush();  
  73.             }  
  74.             //返回值  
  75.             is = conn.getInputStream();  
  76.             return getContent(is, "utf-8");  
  77.         } catch (MalformedURLException e) {  
  78.             is_success = false;  
  79.             e.printStackTrace();  
  80.         } catch (IOException e) {  
  81.             is_success = false;  
  82.             e.printStackTrace();  
  83.         } finally{  
  84.             try {  
  85.                 if(os!=null){os.close();}  
  86.                 if(is!=null){is.close();}  
  87.             } catch (IOException e) {  
  88.                 e.printStackTrace();  
  89.             }  
  90.         }  
  91.         return null;  
  92.     }  

 

 

 

补充:

①post端:

 

[java] 
 
  1. /** 
  2.      * 是否同步成功 
  3.      * 如果能够ping通就表示同步成功,其他逻辑(比如是否真的成功了)在天翼客服完善 
  4.      * syncType:1 insert;2 update;3 delete 
  5.      * @return 
  6.      */  
  7.     public boolean syncIsOk(String dataJson,int syncType){  
  8.   
  9.         InputStreamReader in = null;  
  10.         BufferedReader reader = null;  
  11.         try{  
  12.             if(!ValidateUtils.isEmpty(dataJson)){  
  13.                 String tianyikefu_ip = "http://172.172.3.134:8080";  
  14.   
  15.                 //拼上地址  
  16.                 tianyikefu_ip = tianyikefu_ip + "/manyou/sync_roaming_data.jspx";  
  17.                 System.out.println(tianyikefu_ip);  
  18.                   
  19.                 String url = tianyikefu_ip;       
  20.                 StringBuffer param_xml = new StringBuffer(200);  
  21.                   
  22.                 param_xml.append("{\"dataJson\":"+dataJson+",\"syncType\":"+syncType+"}");  
  23.                 //System.out.println(param_xml);  
  24.                 String xml = HttpRequestUtils.xmlHttpProxy(url, param_xml.toString(), "POST","application/xml");  
  25.                 //System.out.println("返回:" + xml);  
  26.                   
  27.                 /** 
  28.                  * { 
  29.                         "resCode": 200, 
  30.                         "resDesc": "处理成功!" 
  31.                     } 
  32.                  */  
  33.                 JSONObject jsonObject = JSONObject.fromObject(xml);  
  34.                 if(jsonObject!=null&&"200".equals(jsonObject.get("resCode").toString())){  
  35.                     return true;  
  36.                 }  
  37.             }  
  38.         }catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }finally{  
  41.             try{  
  42.                 if(reader!=null){  
  43.                     reader.close();  
  44.                 }  
  45.                   
  46.                 if(in!=null){  
  47.                     in.close();  
  48.                 }  
  49.             }catch (Exception e) {  
  50.                 e.printStackTrace();  
  51.             }  
  52.         }  
  53.         return false;  
  54.     }  


②获取端:

 

 

[java] 
 
    1. StringBuffer info=new StringBuffer();  
    2.                 in = request.getInputStream();  
    3.                 buf = new BufferedInputStream(in);  
    4.                 byte[] buffer=new byte[1024];   
    5.                 int iRead;  
    6.                 while((iRead=buf.read(buffer))!=-1){  
    7.                     info.append(new String(buffer,0,iRead,"gbk"));  
    8.                 }  
    9.   
    10.                 if(info!=null&&!ValidateUtils.isEmpty(info.toString())){  
    11.                     JSONObject paramJson = JSONObject.fromObject(info.toString());  
    12.   
    13.                     /* 
    14.                      * {"dataJson":"","syncType":2} 
    15.                      */  
    16.                     if(paramJson!=null){  
    17.                         dataJson = paramJson.getString("dataJson");  
    18.                         syncType = paramJson.getString("syncType");  
    19.                     }  
    20.                 }  
posted @ 2015-05-05 14:34  leo3689  阅读(289)  评论(0)    收藏  举报