浏览 1885 次
锁定老帖子 主题:请求远程服务传参和过大参数的函数
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2009-10-10
最后修改:2009-10-10
两个Code,第一个函数是普通请求,Like:http://ip:port/server/xxx.do?para1=¶2 第二个函数是隐藏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; } 如有疑问请留言。
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |