package com.fx.util;
import java.util.Vector;
public class HttpResponser {
String urlString;// URL地址串
int defaultPort;
String file;
String host;
String path;
int port;
String protocol;
String query;
String ref;
String userInfo;
String contentEncoding;
String contentString;// 以字符串形式保存 内容
String contentType;
int code;
String message;
String method;// 方法
int connectTimeout;
int readTimeout;
Vector<String> contentCollection;// 以集合形式保存内容,集合中保存行
public String getContentString() {
return contentString;
}
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;
}
}
package com.fx.util;
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 YaoMing
*/
public class HttpRequester {
private String defaultContentEncoding;
public HttpRequester() {
// 得到系统默认的字符编码
this.defaultContentEncoding = Charset.defaultCharset().name();
}
public HttpResponser sendGet(String urlString) throws IOException {
return this.send(urlString, "GET", null, null);
}
public HttpResponser sendGet(String urlString, Map<String, String> params)
throws IOException {
return this.send(urlString, "GET", params, null);
}
public HttpResponser sendGet(String urlString, Map<String, String> params,
Map<String, String> propertys) throws IOException {
return this.send(urlString, "GET", params, propertys);
}
public HttpResponser sendPost(String urlString) throws IOException {
return this.send(urlString, "POST", null, null);
}
public HttpResponser sendPost(String urlString, Map<String, String> params)
throws IOException {
return this.send(urlString, "POST", params, null);
}
public HttpResponser sendPost(String urlString, Map<String, String> params,
Map<String, String> propertys) throws IOException {
return this.send(urlString, "POST", params, propertys);
}
/**
*
* @param urlString
* 地址 应该包含?
* @param method
* 提交方式
* @param parameters
* 参数 传入的参数应该是一个已经通过URL编码以后的参数
* @param propertys
* 请求属性 键值对 例如 key=sun.net.client.defaultConnectTimeout
* value=5000 key=sun.net.client.defaultReadTimeout value=5000
* @return
* @throws IOException
*/
private HttpResponser send(String urlString, String method,
Map<String, String> parameters, Map<String, String> propertys)
throws IOException {
HttpURLConnection urlConnection = null;
URL url = null;
StringBuffer param = new StringBuffer();
if (parameters != null) {
for (String key : parameters.keySet()) {
param.append(key).append("=").append(parameters.get(key));
param.append("&");
}
if (param.length() > 0) {
param = param.deleteCharAt(param.length() - 1);
}
}
url = new URL(urlString + (method.equalsIgnoreCase("GET") ? param : ""));
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(method);
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
if (propertys != null)
for (String key : propertys.keySet()) {
urlConnection.addRequestProperty(key, propertys.get(key));
}
if (method.equalsIgnoreCase("POST")) {
// 要注意:一旦使用了urlConnection.getOutputStream().write()方法,
// urlConnection.setRequestMethod("GET");将失效,其请求方法会自动转为POST
urlConnection.getOutputStream().write(
param.toString().getBytes("UTF-8"));
urlConnection.getOutputStream().flush();
urlConnection.getOutputStream().close();
}
return this.makeContent(urlString, urlConnection);
}
private HttpResponser makeContent(String urlString,
HttpURLConnection urlConnection) throws IOException {
HttpResponser httpResponserText = new HttpResponser();
try {
InputStream in = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(in));
httpResponserText.contentCollection = new Vector<String>();
StringBuffer contentString = new StringBuffer();
String crlf = System.getProperty("line.separator");
String line = bufferedReader.readLine();
while (line != null) {
httpResponserText.contentCollection.add(line);
contentString.append(line).append(crlf);
line = bufferedReader.readLine();
}
bufferedReader.close();
in.close();
// 获得返回值的字符集
String ecod = urlConnection.getContentEncoding();
if (ecod == null)
ecod = this.defaultContentEncoding;
httpResponserText.urlString = urlString;
httpResponserText.defaultPort = urlConnection.getURL()
.getDefaultPort();
httpResponserText.file = urlConnection.getURL().getFile();
httpResponserText.host = urlConnection.getURL().getHost();
httpResponserText.path = urlConnection.getURL().getPath();
httpResponserText.port = urlConnection.getURL().getPort();
httpResponserText.protocol = urlConnection.getURL().getProtocol();
httpResponserText.query = urlConnection.getURL().getQuery();
httpResponserText.ref = urlConnection.getURL().getRef();
httpResponserText.userInfo = urlConnection.getURL().getUserInfo();
httpResponserText.contentString = new String(contentString
.toString().getBytes(), ecod);
httpResponserText.contentEncoding = ecod;
httpResponserText.code = urlConnection.getResponseCode();
httpResponserText.message = urlConnection.getResponseMessage();
httpResponserText.contentType = urlConnection.getContentType();
httpResponserText.method = urlConnection.getRequestMethod();
httpResponserText.connectTimeout = urlConnection
.getConnectTimeout();
httpResponserText.readTimeout = urlConnection.getReadTimeout();
return httpResponserText;
} catch (IOException e) {
throw e;
} finally {
// 最终关闭流
if (urlConnection != null)
urlConnection.disconnect();
}
}
/**
*
* @param urlPath eg:http://www.viralpatel.net/blogs/
* @return
*/
public String getResponseString(String urlPath) {
try {
URL url = new URL(urlPath);
BufferedReader br = new BufferedReader(new InputStreamReader(url
.openStream()));
StringBuffer sb = new StringBuffer();
String tp=br.readLine();
while (tp!= null) {
sb.append(tp);
tp=br.readLine();
}
return sb.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public String getDefaultContentEncoding() {
return this.defaultContentEncoding;
}
public void setDefaultContentEncoding(String contentEncoding) {
this.defaultContentEncoding = contentEncoding;
}
}
package com.fx.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class HttpCheck {
/**
* 将参数先进行base64编码 在进行URL编码
* @param httpUrlstr
* @param param
* @return
*/
public static String check(String httpUrlstr, String param){
HttpRequester request = new HttpRequester();
HttpResponser hr = null;
BASE64Decoder bd = new BASE64Decoder();
BASE64Encoder be= new BASE64Encoder();
String req="";
try {
req = URLEncoder.encode(be.encode((param).getBytes("utf-8")),"utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
return null;
}
try {
hr = request.sendGet(httpUrlstr+req);
String resposne=new String(bd.decodeBuffer(URLDecoder.decode(hr.getContentString(), "utf-8")),"utf-8");
return resposne;
} catch (Exception e) {
String e_str = "Send get to " + httpUrlstr + " error : " + e.toString();
System.err.println(e_str);
return null;
}
}
public static boolean exits(String http) {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(http).openConnection();
connection.setConnectTimeout(1000);
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
} catch (IOException e) {
return false;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
分享到:
相关推荐
《深入解析httpclient.jar及其与code.jar的关联》 在Java开发中,HTTP通信是不可或缺的一部分,而Apache HttpClient库正是Java实现HTTP客户端操作的重要工具。本文将深入探讨httpclient.jar包,以及它与code.jar包...
HttpClient 4.2.1版本引入了一些重要的改进和修复,以提高性能和稳定性。以下是一些关键特性: 1. **连接管理**:HttpClient 4.2.1引入了更完善的连接管理机制,允许开发者控制连接的创建、复用和关闭。`...
### Httpclient官网教程中文版知识点总结 #### 一、引言 HTTP协议作为互联网的核心通信标准之一,在现代网络服务及物联网设备中扮演着至关重要的角色。随着技术的发展,越来越多的应用和服务依赖于HTTP协议来实现...
例如,在HttpClient 3.x中,代码可能会使用`***mons.httpclient.HttpClient`类和`***mons.httpclient.methods.GetMethod`等,而在4.x版本中,这些都被新的API所替代。程序员需要熟悉`org.apache....
赠送jar包:httpclient-4.2.5.jar; 赠送原API文档:httpclient-4.2.5-javadoc.jar; 赠送源代码:httpclient-4.2.5-sources.jar; 赠送Maven依赖信息文件:httpclient-4.2.5.pom; 包含翻译后的API文档:httpclient...
本文将深入探讨HttpClient 4.2.1的核心特性和使用方法,帮助开发者更好地理解和应用这个强大的工具。 一、HttpClient简介 HttpClient是一个开放源码的Java库,由Apache软件基金会维护。它为Java程序员提供了一个...
HttpClient httpClient = new HttpClient(); // 设置 Http 连接超时为5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); /* 2 生成 GetMethod 对象并设置参数 */ GetMethod ...
赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...
HttpClientHelper 对这个类进行了封装,使得开发者无需直接与HttpClient接口打交道,而是通过更简洁、易用的方法调用来实现网络通信。这提高了代码的可读性和可维护性。 单例模式是软件设计模式的一种,确保一个类...
本篇文章将深入探讨如何使用HttpClient方式调用URL,以及相关的知识点。 首先,HttpClient允许我们构建复杂的HTTP请求,包括GET、POST以及其他HTTP方法。使用HttpClient调用URL的基本步骤包括创建HttpClient实例、...
HttpClient 4.13版本是这个库的一个较新版本,包含了一系列的改进和修复。 在Java开发中,HttpClient是一个常用的工具,尤其在处理Web服务或者API调用时。它支持同步和异步操作,可以处理复杂的HTTP协议细节,使...
这个实例主要涉及如何配置HttpClient来忽略SSL(Secure Socket Layer)验证,这对于在开发和测试环境中处理自签名证书或未认证的服务器非常有用。以下将详细介绍HttpClient的使用以及如何进行SSL验证的忽略。 首先...
《HttpClient 4.5详解与应用实践》 HttpClient是一个开源的Java库,由Apache软件基金会维护,主要用于在HTTP协议上实现客户端的通信。版本4.5是HttpClient的一个稳定版本,提供了许多增强的功能和优化,使其成为...
本压缩包文件"httpClient"很可能包含了HttpClient库所需的必备JAR文件,这些文件通常包括HttpClient的核心库、依赖的第三方库以及可能的扩展模块。为了正确使用HttpClient,你需要确保将这些JAR文件添加到你的项目类...
HTTPClient 4.5是该库的一个稳定版本,它引入了一些重要的改进和新特性,旨在提高性能、可靠性和易用性。以下是对标题和描述中涉及的HTTPClient-4.5所需jar包的详细解释: 1. **httpclient-4.5.jar**:这是...
赠送jar包:httpclient-4.4.1.jar; 赠送原API文档:httpclient-4.4.1-javadoc.jar; 赠送源代码:httpclient-4.4.1-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.1.pom; 包含翻译后的API文档:httpclient...
《深入理解HTTPClient 4.5及其依赖》 在Java编程世界中,HTTPClient是一个非常重要的库,它允许开发者执行HTTP请求并处理响应。本文将深入探讨`httpclient-4.5.jar`这个包,以及它所依赖的相关jar包,帮助你更好地...
赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...
在本文中,我们将深入探讨如何使用HttpClient调用WebService。 首先,调用WebService通常涉及SOAP(Simple Object Access Protocol)或RESTful API。HttpClient可以处理这两种类型的Web服务。在本示例中,我们假设...