`

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

阅读更多

    两个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;
	}

    如有疑问请留言。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics