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 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...
赠送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文档:...
赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...
赠送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文档:...
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....
httpclient-4.5所需jar包,里面包含httpclient-4.5.jar等等10个必须的开发包。 1.commons-codec-1.9.jar 2.commons-logging-1.2.jar 3.fluent-hc-4.5.jar 4.httpclient-4.5.jar 5.httpclient-cache-4.5.jar 6....
HttpClient 4.5.3 (GA) is a maintenance release that fixes a number of defects found since 4.5.2. Please note that as of 4.4 HttpClient requires Java 1.6 or newer. Changelog: ------------------- * ...
httpclient.jar下载 包括code.jar包
try(CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8")); ...
Android使用HttpClient发送请求、接收响应很简单,只要如下几步即可: Step1:创建HttpClient对象; Step2:如果需要发送GET请求,则创建HttpGet对象; 如果需要发送POST请求,则创建HttpPost对象; Step3:如果...
赠送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...
赠送jar包:httpclient-4.5.10.jar; 赠送原API文档:httpclient-4.5.10-javadoc.jar; 赠送源代码:httpclient-4.5.10-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.10.pom; 包含翻译后的API文档:...
压缩包中含有多个文档,从了解httpclient到应用。 httpClient 1httpClint 1.1简介 HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持...
赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)版.zip ...
赠送jar包:httpclient-4.4.jar; 赠送原API文档:httpclient-4.4-javadoc.jar; 赠送源代码:httpclient-4.4-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.pom; 包含翻译后的API文档:httpclient-4.4-...
帮助程序员快速从Apache的HttpClient 3.x升级到HttpClient 4.x
赠送jar包:httpclient-4.5.3.jar 赠送原API文档:httpclient-4.5.3-javadoc.jar 赠送源代码:httpclient-4.5.3-sources.jar 包含翻译后的API文档:httpclient-4.5.3-javadoc-API文档-中文(简体)版.zip 对应Maven...