java基于HTTP协议post传参,获取参数

原文地址为: java基于HTTP协议post传参,获取参数






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");
					}
				}




转载请注明本文地址: java基于HTTP协议post传参,获取参数
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值