两个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;
}
如有疑问请留言。
分享到:
- 2009-10-10 12:26
- 浏览 1254
- 评论(0)
- 论坛回复 / 浏览 (0 / 1848)
- 查看更多
相关推荐
connect函数的几个用法 通过分别演示信号有无参数与对应的有无参数的槽函数 能够很好地了解connect的用法 【包括对lambda函数的应用】
bat批处理,指定文件夹,通过函数传参
NULL 博文链接:https://youzhibing.iteye.com/blog/2166047
专门讲解函数参数的传递的。希望学学,有很大的帮助
引用改网友的 https://download.csdn.net/download/jackeywjf/1521390 看懂后对其改进和中文注释,C#动态调用DLL并对目标函数传递参数
使用window.open()传递参数 使用redirect到html页面 js获取参数函数
python函数和函数传参ppt.pptx
怎么往SetTimer的回调函数传递参数
定时器事件的响应函数,即 VOID CALLBACK TimerProc() 不是由用户程序自己调用的,而是根据时钟事件借由操作系统内核调用的;那么,如何利用这些形式参数,传递程序逻辑处理所需要的参数呢?本文给出一个解决方案,...
python函数和函数传参ppt.zip
将多个变量放到一个结构体中,减少函数传递时的多个参数传进传出的复杂性 结构体传进函数时,是以引用的形式传入的,不是以指针的形式。
Linux运维-嵌入式物联网开发教程-函数传参之按值传递参数.mp4
Linux运维-嵌入式物联网开发教程-函数传参之按地址传递参数.mp4
//设置按钮的(x,y)坐标、长、宽//设置按键上显示的文字//将信号和槽连接connect(push, SIGNAL(clicked()), this, SL
WMI获取远程服务器CPU,内存使用率函数源代码,主要解决Win32_LogicalMemoryConfiguration不能用的问题
C# 获取调用函数 参数名称和值C# 获取调用函数 参数名称和值C# 获取调用函数 参数名称和值C# 获取调用函数 参数名称和值C# 获取调用函数 参数名称和值C# 获取调用函数 参数名称和值