[转载]java中用URLConnection 类post方式提交表单
/**
* <pre>
* Title: HttpRequestProxy.java
* Project: HP-Common
* Type: com.hengpeng.common.web.HttpRequestProxy
* Author: benl
* Create: 2007-7-3 上午03:07:07
* Copyright: Copyright (c) 2007
* Company:
* <pre>
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.log4j.Logger;
/**
* <pre>
* HTTP请求代理类
* </pre>
*
* @author benl
* @version 1.0, 2007-7-3
*/
public class HttpRequestProxy
{
/**
* 连接超时
*/
private static int connectTimeOut = 5000;
/**
* 读取数据超时
*/
private static int readTimeOut = 10000;
/**
* 请求编码
*/
private static String requestEncoding = "GBK";
private static Logger logger = Logger.getLogger(HttpRequestProxy.class);
/**
* <pre>
* 发送带参数的GET的HTTP请求
* </pre>
*
* @param reqUrl HTTP请求URL
* @param parameters 参数映射表
* @return HTTP响应的字符串
*/
public static String doGet(String reqUrl, Map parameters,
String recvEncoding)
{
HttpURLConnection url_con = null;
String responseContent = null;
try
{
StringBuffer params = new StringBuffer();
for (Iterator iter = parameters.entrySet().iterator(); iter
.hasNext();)
{
Entry element = (Entry) iter.next();
params.append(element.getKey().toString());
params.append("=");
params.append(URLEncoder.encode(element.getValue().toString(),
HttpRequestProxy.requestEncoding));
params.append("&");
}
if (params.length() > 0)
{
params = params.deleteCharAt(params.length() - 1);
}
URL url = new URL(reqUrl);
url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestMethod("GET");
System.setProperty("sun.net.client.defaultConnectTimeout", String
.valueOf(HttpRequestProxy.connectTimeOut));// (单位:毫秒)jdk1.4换成这个,连接超时
System.setProperty("sun.net.client.defaultReadTimeout", String
.valueOf(HttpRequestProxy.readTimeOut)); // (单位:毫秒)jdk1.4换成这个,读操作超时
// url_con.setConnectTimeout(5000);//(单位:毫秒)jdk
// 1.5换成这个,连接超时
// url_con.setReadTimeout(5000);//(单位:毫秒)jdk 1.5换成这个,读操作超时
url_con.setDoOutput(true);
byte[] b = params.toString().getBytes();
url_con.getOutputStream().write(b, 0, b.length);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in,
recvEncoding));
String tempLine = rd.readLine();
StringBuffer temp = new StringBuffer();
String crlf=System.getProperty("line.separator");
while (tempLine != null)
{
temp.append(tempLine);
temp.append(crlf);
tempLine = rd.readLine();
}
responseContent = temp.toString();
rd.close();
in.close();
}
catch (IOException e)
{
logger.error("网络故障", e);
}
finally
{
if (url_con != null)
{
url_con.disconnect();
}
}
return responseContent;
}
/**
* <pre>
* 发送不带参数的GET的HTTP请求
* </pre>
*
* @param reqUrl HTTP请求URL
* @return HTTP响应的字符串
*/
public static String doGet(String reqUrl, String recvEncoding)
{
HttpURLConnection url_con = null;
String responseContent = null;
try
{
StringBuffer params = new StringBuffer();
String queryUrl = reqUrl;
int paramIndex = reqUrl.indexOf("?");
if (paramIndex > 0)
{
queryUrl = reqUrl.substring(0, paramIndex);
String parameters = reqUrl.substring(paramIndex + 1, reqUrl
.length());
String[] paramArray = parameters.split("&");
for (int i = 0; i < paramArray.length; i++)
{
String string = paramArray[i];
int index = string.indexOf("=");
if (index > 0)
{
String parameter = string.substring(0, index);
String value = string.substring(index + 1, string
.length());
params.append(parameter);
params.append("=");
params.append(URLEncoder.encode(value,
HttpRequestProxy.requestEncoding));
params.append("&");
}
}
params = params.deleteCharAt(params.length() - 1);
}
URL url = new URL(queryUrl);
url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestMethod("GET");
System.setProperty("sun.net.client.defaultConnectTimeout", String
.valueOf(HttpRequestProxy.connectTimeOut));// (单位:毫秒)jdk1.4换成这个,连接超时
System.setProperty("sun.net.client.defaultReadTimeout", String
.valueOf(HttpRequestProxy.readTimeOut)); // (单位:毫秒)jdk1.4换成这个,读操作超时
// url_con.setConnectTimeout(5000);//(单位:毫秒)jdk
// 1.5换成这个,连接超时
// url_con.setReadTimeout(5000);//(单位:毫秒)jdk 1.5换成这个,读操作超时
url_con.setDoOutput(true);
byte[] b = params.toString().getBytes();
url_con.getOutputStream().write(b, 0, b.length);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in,
recvEncoding));
String tempLine = rd.readLine();
StringBuffer temp = new StringBuffer();
String crlf=System.getProperty("line.separator");
while (tempLine != null)
{
temp.append(tempLine);
temp.append(crlf);
tempLine = rd.readLine();
}
responseContent = temp.toString();
rd.close();
in.close();
}
catch (IOException e)
{
logger.error("网络故障", e);
}
finally
{
if (url_con != null)
{
url_con.disconnect();
}
}
return responseContent;
}
/**
* <pre>
* 发送带参数的POST的HTTP请求
* </pre>
*
* @param reqUrl HTTP请求URL
* @param parameters 参数映射表
* @return HTTP响应的字符串
*/
public static String doPost(String reqUrl, Map parameters,
String recvEncoding)
{
HttpURLConnection url_con = null;
String responseContent = null;
try
{
StringBuffer params = new StringBuffer();
for (Iterator iter = parameters.entrySet().iterator(); iter
.hasNext();)
{
Entry element = (Entry) iter.next();
params.append(element.getKey().toString());
params.append("=");
params.append(URLEncoder.encode(element.getValue().toString(),
HttpRequestProxy.requestEncoding));
params.append("&");
}
if (params.length() > 0)
{
params = params.deleteCharAt(params.length() - 1);
}
URL url = new URL(reqUrl);
url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestMethod("POST");
System.setProperty("sun.net.client.defaultConnectTimeout", String
.valueOf(HttpRequestProxy.connectTimeOut));// (单位:毫秒)jdk1.4换成这个,连接超时
System.setProperty("sun.net.client.defaultReadTimeout", String
.valueOf(HttpRequestProxy.readTimeOut)); // (单位:毫秒)jdk1.4换成这个,读操作超时
// url_con.setConnectTimeout(5000);//(单位:毫秒)jdk
// 1.5换成这个,连接超时
// url_con.setReadTimeout(5000);//(单位:毫秒)jdk 1.5换成这个,读操作超时
url_con.setDoOutput(true);
byte[] b = params.toString().getBytes();
url_con.getOutputStream().write(b, 0, b.length);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in,
recvEncoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf=System.getProperty("line.separator");
while (tempLine != null)
{
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
}
catch (IOException e)
{
logger.error("网络故障", e);
}
finally
{
if (url_con != null)
{
url_con.disconnect();
}
}
return responseContent;
}
/**
* @return 连接超时(毫秒)
* @see com.hengpeng.common.web.HttpRequestProxy#connectTimeOut
*/
public static int getConnectTimeOut()
{
return HttpRequestProxy.connectTimeOut;
}
/**
* @return 读取数据超时(毫秒)
* @see com.hengpeng.common.web.HttpRequestProxy#readTimeOut
*/
public static int getReadTimeOut()
{
return HttpRequestProxy.readTimeOut;
}
/**
* @return 请求编码
* @see com.hengpeng.common.web.HttpRequestProxy#requestEncoding
*/
public static String getRequestEncoding()
{
return requestEncoding;
}
/**
* @param connectTimeOut 连接超时(毫秒)
* @see com.hengpeng.common.web.HttpRequestProxy#connectTimeOut
*/
public static void setConnectTimeOut(int connectTimeOut)
{
HttpRequestProxy.connectTimeOut = connectTimeOut;
}
/**
* @param readTimeOut 读取数据超时(毫秒)
* @see com.hengpeng.common.web.HttpRequestProxy#readTimeOut
*/
public static void setReadTimeOut(int readTimeOut)
{
HttpRequestProxy.readTimeOut = readTimeOut;
}
/**
* @param requestEncoding 请求编码
* @see com.hengpeng.common.web.HttpRequestProxy#requestEncoding
*/
public static void setRequestEncoding(String requestEncoding)
{
HttpRequestProxy.requestEncoding = requestEncoding;
}
public static void main(String[] args)
{
Map map = new HashMap();
map.put("actionType", "1");
// map.put("issueId", "33");
String temp = HttpRequestProxy.doPost("http://192.168.0.99/AgentPortal/autoHandler", map, "GBK");
System.out.println("返回的消息是:"+temp);
}
}
分享到:
相关推荐
java中用URLConnection类post方式提交表单.pdf
java中用URLConnection类post方式提交表单的方法。
java 提供了两个类,在这两个类里封装了大部分 Web 相关的各种操作。这两个类是 URL 类和 URLConnection 类
NULL 博文链接:https://xiaolinjava.iteye.com/blog/1195523
NULL 博文链接:https://bijian1013.iteye.com/blog/2299735
java URLConnection 的详细介绍,牛人项目经验的总结,绝对通俗易懂,包括cookie操作,上传文件,以及下载网页等介绍,是非常难得文档和教材
java URLConnection的总结,简单易懂,并有详细的代码作为例子,非常容易学习和模仿
下面小编就为大家分享一篇基于java URL和URLConnection(详解),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
java URLConnection全面总结(包括cookie的详细操作)
本篇文章主要介绍了Java两种方式简单实现:爬取网页并且保存 ,主要用UrlConnection、HttpClient爬取实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
安卓URLConnection进行网络编程,一个简单的例子,服务器也有~
Java自带的HttpURLConnection访问接口文件上传, 含普通参数值传递和文件传递
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和URL之间的通信连接。程序可以通过URLConnection实例向该URL发送请求,读取URL引用的资源。 程序Demo示范了如何向Web站点发送GET请求、...
。。。
import java.net.URLConnection; /** * 使用URLConnection下载文件或图片并保存到本地。 * * @author 老紫竹(laozizhu.com) */ public class URLConnectionDownloader { public static void main...
主要介绍了Java 必知必会的 URL 和 URLConnection使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
用于对基于HTTP的服务进行抓取 HttpClient 现在是HttpComponents 把问题搞复杂了 其实Java内置的HttpURLConnection很好的 内含文档 使用方法见http://blog.csdn.net/jdgdf566/article/details/17039693
URLConnection和HttpClient使用入门
URLConnection调用POST请求和GET请求封装好的工具类,json数据传递 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从java.net.URL得到, 然后把连接设为输出模式。URLConnection通常作为输入来...