请求远程服务传参和过大参数的函数

    两个Code,第一个函数是普通请求,Like:http://ip:port/server/xxx.do?para1=&para2

    第二个函数是隐藏parameter的请求,同样也可以提交大于255字节的URL请求。(IE和Firefox要求不同),Like:http://ip:port/server/xxx.do.

 

public String postServerUrl(Object obj,String postMethod) throws IOException, Throwable, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
		// Get and config the connection.		
		HttpURLConnection conn;
		URL requestServiceUrl = new URL(getWebServerUrl()+postMethod+"?"+covertParameter(obj));
		System.out.println(requestServiceUrl.toString());		
		conn = (HttpURLConnection) requestServiceUrl.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestMethod("POST");
		conn.setUseCaches(false);		
		conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");		
		conn.setInstanceFollowRedirects(true);
		conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		// Get the response.
		InputStream is = conn.getInputStream();
		String rc = IOUtils.toString(is);
		return rc;
	}

 第二个,其中参数内定为:questionData,相当于本地的Form把一个大对象提交到远程服务器上。

public String postProcessResponseUrl(String questionData,String postMethod) throws IOException, Throwable, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{
		// Get and config the connection.		
		HttpURLConnection conn;
		URL requestServiceUrl = new URL(getWebServerUrl()+postMethod);
		System.out.println(requestServiceUrl.toString());		
		conn = (HttpURLConnection) requestServiceUrl.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestMethod("POST");
		conn.setUseCaches(false);		
		conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
		
		conn.setInstanceFollowRedirects(true);
		conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		// Get the response.
		PrintWriter out = new PrintWriter(conn.getOutputStream());
		String name = "questionData="+URLEncoder.encode(questionData, "UTF-8"); 
		System.out.println(name);
        // send the encoded message
        out.println(name);
        out.close();
		InputStream is = conn.getInputStream();
		String rc = IOUtils.toString(is);
		return rc;
	}

   好了,还有一个使用到的函数:(使用了注射机制)

private String covertParameter(Object obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		String Parameter = "";
		Field[] fields = obj.getClass().getFields();
		for (int i=0;i<fields.length;i++){
			String fileldName= fields[i].getName();
			Method getMethod = obj.getClass().getMethod("get"+fileldName.substring(0,1).toUpperCase()+fileldName.substring(1), new Class[]{});
			Object value = getMethod.invoke(obj, new Object[]{});
			value = value==null?"":value;
			if (i>0){
				Parameter+="&";
			}			
			Parameter += fields[i].getName()+"="+value.toString();
		}		
		return Parameter;
	}

    如有疑问请留言。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值