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.nio.charset.Charset;
import java.util.Map;
import java.util.Vector;
/**
* HTTP请求对象
*
* @author 尹浩
*/
public class HttpRequester {
private String defaultContentEncoding;
public HttpRequester() {
this.defaultContentEncoding = Charset.defaultCharset().name();
}
public HttpRespons sendGet(String urlString) throws IOException {
return this.send(urlString, "GET", null, null);
}
/**
* * 发送GET请求 * * @param urlString * URL地址 * @param params
* * 参数集合 * @return 响应对象 * @throws IOException
*/
public HttpRespons sendGet(String urlString, Map<String, String> params)
throws IOException {
return this.send(urlString, "GET", params, null);
}
/**
* * 发送GET请求 * * @param urlString * URL地址 * @param params
* * 参数集合 * @param propertys * 请求属性 * @return 响应对象 * @throws
* IOException
*/
public HttpRespons sendGet(String urlString, Map<String, String> params,
Map<String, String>
propertys) throws IOException {
return this.send(urlString, "GET", params, propertys);
}
/**
* * 发送POST请求 * * @param urlString * URL地址 * @return 响应对象
* * @throws IOException
*/
public HttpRespons sendPost(String urlString) throws IOException {
return this.send(urlString, "POST", null, null);
}
/**
* * 发送POST请求 * * @param urlString * URL地址 * @param params
* * 参数集合 * @return 响应对象 * @throws IOException
*/
public HttpRespons sendPost(String urlString, Map<String, String> params)
throws IOException {
return this.send(urlString, "POST", params, null);
}
/**
* * 发送POST请求 * * @param urlString * URL地址 * @param params
* * 参数集合 * @param propertys * 请求属性 * @return 响应对象 * @throws
* IOException
*/
public HttpRespons sendPost(String urlString, Map<String, String> params,
Map<String, String>
propertys) throws IOException {
return this.send(urlString, "POST", params, propertys);
}
/**
* * 发送HTTP请求 * * @param urlString * @return 响映对象 * @throws
* IOException
*/
private HttpRespons send(String urlString, String method,
Map<String, String> parameters,
Map<String, String> propertys) throws IOException {
// HttpURLConnection为局部变量
HttpURLConnection urlConnection = null;
// URL对象
URL url = null;
// 如果请求为GET方法,并且参数不为空
if (method.equalsIgnoreCase("GET") && parameters != null) {
// 构建并拼接参数字符串
StringBuffer param = new StringBuffer();
int i = 0;
for (String key : parameters.keySet()) {
if (i == 0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(parameters.get(key));
i++;
}
// 拼接URL串 + 参数
urlString += param;
}
// NEW一个URL对象,由该对象的openConnection()方法将生成一个URLConnection对象
url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
// 设置相关属性,具体含义请查阅JDK文档
urlConnection.setRequestMethod(method);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
// 赋予请求属性
if (propertys != null)
for (String key : propertys.keySet()) {
urlConnection.addRequestProperty(key, propertys.get(key));
}
// 如果请求为POST方法,并且参数不为空
if (method.equalsIgnoreCase("POST") && parameters != null) {
StringBuffer param = new StringBuffer();
for (String key : parameters.keySet()) {
param.append("&");
param.append(key).append("=").append(parameters.get(key));
}
// 将参数信息发送到HTTP服务器
// 要注意:一旦使用了urlConnection.getOutputStream().write()方法,
urlConnection.setRequestMethod("GET");
urlConnection.getOutputStream().write(param.toString().getBytes());
urlConnection.getOutputStream().flush();
urlConnection.getOutputStream().close();
}
return this.makeContent(urlString, urlConnection);
}
/**
* * 得到响应对象 * * @param urlConnection * @return 响应对象 * @throws
* IOException
*/
private HttpRespons makeContent(String urlString,
HttpURLConnection urlConnection) throws IOException
{
HttpRespons httpResponser = new HttpRespons();
try {
// 得到响应流
InputStream in = urlConnection.getInputStream();
// 封装成高级对象
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(in));
// 内容集合(集合项为行内容)
httpResponser.contentCollection = new Vector<String>();
StringBuffer temp = new StringBuffer();
String line = bufferedReader.readLine();
while (line != null) {
httpResponser.contentCollection.add(line);
temp.append(line).append("\r\n");
line = bufferedReader.readLine();
}
bufferedReader.close();
// 得到请求连接的字符集
String ecod = urlConnection.getContentEncoding();
if (ecod == null)
ecod = this.defaultContentEncoding;
// 将各属性赋值给响应对象
httpResponser.urlString = urlString;
httpResponser.defaultPort = urlConnection.getURL().getDefaultPort();
httpResponser.file = urlConnection.getURL().getFile();
httpResponser.host = urlConnection.getURL().getHost();
httpResponser.path = urlConnection.getURL().getPath();
httpResponser.port = urlConnection.getURL().getPort();
httpResponser.protocol = urlConnection.getURL().getProtocol();
httpResponser.query = urlConnection.getURL().getQuery();
httpResponser.ref = urlConnection.getURL().getRef();
httpResponser.userInfo = urlConnection.getURL().getUserInfo();
httpResponser.content = new String(temp.toString().getBytes(), ecod);
httpResponser.contentEncoding = ecod;
httpResponser.code = urlConnection.getResponseCode();
httpResponser.message = urlConnection.getResponseMessage();
httpResponser.contentType = urlConnection.getContentType();
httpResponser.method = urlConnection.getRequestMethod();
httpResponser.connectTimeout = urlConnection.getConnectTimeout();
httpResponser.readTimeout = urlConnection.getReadTimeout();
return httpResponser;
} catch (IOException e) {
throw e;
} finally {
// 最终关闭流
if (urlConnection != null)
urlConnection.disconnect();
}
}
/**
* 默认的响应字符集
*/
public String getDefaultContentEncoding() {
return this.defaultContentEncoding;
}
/**
* 设置默认的响应字符集
*/
public void setDefaultContentEncoding(String defaultContentEncoding) {
this.defaultContentEncoding = defaultContentEncoding;
}
}
import java.util.Vector;
/**
*
* @author 尹浩
*
*/
public class HttpRespons {
String urlString;// URL地址串
int defaultPort;
String file;
String host;
String path;
int port;
String protocol;
String query;
String ref;
String userInfo;
String contentEncoding;
String content;// 内容
String contentType;
int code;
String message;
String method;// 方法
int connectTimeout;
int readTimeout;
Vector<String> contentCollection;// 内容,集合中保存行
public String getContent() {
return content;
}
public String getContentType() {
return contentType;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public Vector<String> getContentCollection() {
return contentCollection;
}
public String getContentEncoding() {
return contentEncoding;
}
public String getMethod() {
return method;
}
public int getConnectTimeout() {
return connectTimeout;
}
public int getReadTimeout() {
return readTimeout;
}
public String getUrlString() {
return urlString;
}
public int getDefaultPort() {
return defaultPort;
}
public String getFile() {
return file;
}
public String getHost() {
return host;
}
public String getPath() {
return path;
}
public int getPort() {
return port;
}
public String getProtocol() {
return protocol;
}
public String getQuery() {
return query;
}
public String getRef() {
return ref;
}
public String getUserInfo() {
return userInfo;
}
}
public class Test {
public static void main(String[] ars) {
HttpRequester request = new HttpRequester();
HttpRespons hr = null;
String urlStr = "http://hi.baidu.com/yymmiinngg";
try {
hr = request.sendGet(urlStr);
System.out.println(hr.getContent());
} catch (Exception e) {
String e_str = "Send get to " + urlStr + " error : " + e.toString();
}
}
}
分享到:
相关推荐
Java发送http请求、https请求,post和get都支持
java发送http/https请求(get/post)代码,java 通过发送json,post请求,返回json数据的方法 java 通过发送json,post请求,返回json数据的方法
java发送http请求的一个小例子 包含get和post两种请求方式
Java发送Http请求,解析html返回
java发送http请求报文json
这是一个java发送get、post请求,并得到返回结果的工具类。
JAVA发送HTTP请求,返回HTTP响应内容 首先让我们先构建一个请求类(HttpRequester)。 该类封装了JAVA实现简单请求的代码。 其次我们来看看响应对象(HttpRespons)。响应对象其实只是一个数据BEAN,由此来封装请求...
完整的java发送http包工程,可发送get及post请求。
下面小编就为大家带来一篇java发送http请求并获取状态码的简单实例。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
JAVA发送HTTP请求操作类 HttpRequester request = new HttpRequester(); HttpRespons hr = request.sendPost("响应地址", 参数Map);//有重载,可设置请求头、请求体 hr获得回执内容
声明式HTTP客户端API框架,让Java发送HTTP/HTTPS请求不再难。它比OkHttp和HttpClient更高层,是封装调用第三方restful api client接口的好帮手,是retrofit和feign之外另一个选择。通过在接口上声明注解的方式配置...
主要介绍了JAVA发送HTTP请求,返回HTTP响应内容,应用及实例代码,需要的朋友可以参考下
java 发送http和https请求的实例.docx
用java编写了http Post的请求代码,通过发送请求的 URL,获取远程资源的响应结果,入参为json字符串。使用到httpPost,CloseableHttpClient
java http 发送xml报文(java发送xml报文实例+参数)java http 发送xml报文java http 发送xml报文(java发送xml报文实例+参数)
自己项目里的,可用
声明式HTTP客户端API框架,让Java发送HTTP/HTTPS请求不再难。它比OkHttp和HttpClient更高层,是封装调用第三方restful api client接口的好帮手,是retrofit和feign之外另一个选择。通过在接口上声明注解的方式配置...
下面小编就为大家带来一篇java发送http get请求的两种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Java发送http get/post请求调用接口/方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧