`

java url与httpclient

    博客分类:
  • java
 
阅读更多

使用java客户端访问网站是程序猿必备的技能,java默认的包java.net就有支持

 

package Http.client;

import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;

/**
 * 类说明 使用官网的net类进行测试
 * 
 * @author rfk
 */
public class JavaNet {
	@SuppressWarnings("resource")
	public static void main(String args[]) {
		String urla = "https://www.baidu.com/";
		URL myUrl = null;
		try {
			myUrl = new URL(urla);
			InputStream input = myUrl.openStream();
			Scanner scan = new Scanner(input);
			scan.useDelimiter("\n");
			while (scan.hasNext()) {
				String str = scan.next();
				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

此处使用的是get请求,地址拼接的方式,如果使用post请求,则需要设置表头。

 

package Http.client;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 类说明
 * 
 * @author rfk
 */
public class TestHttpUrl {
	public static void main(String[] args) {
		URL url = null;
		try {
			url = new URL("http://localhost:9180/" + "abcde");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		HttpURLConnection connection = null;
		try {
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type", "application/json");
			connection.connect();
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			String str = "传输的数据";
			out.writeBytes(str);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			StringBuffer buffer = new StringBuffer("");
			String lines;
			while ((lines = reader.readLine()) != null) {
				buffer.append(lines);
				reader.close();
				connection.disconnect();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

观察代码发现很麻烦,所以引用httpclient

 

package Http.client;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 类说明
 * 
 * @author rfk
 */
public class TestHttpClientGet {
	public static void main(String[] args) {
		// 如果此处String url="www.baidu.com"; 会出现错误
		String url = "http://www.baidu.cn/";
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = null;
		try {
			response = client.execute(httpGet);
			int code = response.getStatusLine().getStatusCode();
			if (code == 200) {
				String str = EntityUtils.toString(response.getEntity());
				System.err.println(str);
				File fi = new File("/Users/sona/Desktop/a.txt");
				if (fi.getParent() != null) {
					if (!fi.exists())
						fi.createNewFile();
					BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fi));
					out.write(str.getBytes());
					out.flush();
					out.close();
				} else {
					try {
						throw new Exception();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

 

如果是post请求,则代码如下

package Http.client;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

/**
 * 类说明
 * 
 * @author rfk
 */
public class TestHttpClientPost {
	public final static String url = "http://www.baidu.com";
	// 需要访问的地址
	public static void main(String[] args) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		// 创建一个HTTP访问的客户端
		HttpPost httpPost = new HttpPost(url);
		// 现在即将发送一个post请求 NameValuePair是一个接口
		List<NameValuePair> allParams = new ArrayList<NameValuePair>();
		// 需要将所有的请求参数进行一个封装的处理操作
		// public class BasicNameValuePair implements NameValuePair
		allParams.add(new BasicNameValuePair("msg", "世界,你好!"));
		// 追加传递参数
		allParams.add(new BasicNameValuePair("mid", "helloworld"));
		// 追加传递参数
		// 由于现在传递了中文,所以需要针对于中文进行编码的控制
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(allParams, "UTF-8");// 设置拜尼妈
		httpPost.setEntity(entity);
		// 将需要发送的数据与POST请求对象绑定在一起
		CloseableHttpResponse response = httpClient.execute(httpPost); // 发送GET请求
		System.out.println(response.getEntity());
		int status = response.getStatusLine().getStatusCode();
		if (status >= 200 && status < 300) {
			InputStream input = response.getEntity().getContent();
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] data = new byte[1024];
			int len = 0;
			while ((len = input.read(data)) != -1) {
				bos.write(data, 0, len);
			}
			System.out.println(new String(bos.toByteArray()));
		} else {
			throw new ClientProtocolException("Unexpected response status: " + status);
		}
	}
}

如果需要在java客户段上传文件

 

package Http.client;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.HttpClients;

public class HttpClientUploadDemo {
	public static void main(String[] args) throws Exception {
		//需要上传的文件
		File file = new File("D:" + File.separator + "dog.jpg");
		//路径
		String url = "http://www.baidu.com";
		// 创建一个HttpClient操作类
		HttpClient httpClient = HttpClients.createDefault();
		//创建post
		HttpPost httpPost = new HttpPost(url);
		// 如果要上传文件则一定要使用“multipart/form-data”进行设置
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.addPart("photo", new FileBody(file, ContentType.create("image/jpeg")));
		builder.addPart("msg", new StringBody("世界,你好", ContentType.create("text/plain", Consts.UTF_8)));
		HttpEntity entity = builder.build(); // 定义上传实体类
		httpPost.setEntity(entity);
		HttpResponse response = httpClient.execute(httpPost); // 发送post请求
		System.out.println(response.getEntity());
		System.out.println(response.getStatusLine().getStatusCode());
		InputStream input = response.getEntity().getContent();
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int len = 0;
		while ((len = input.read(data)) != -1) {
			bos.write(data, 0, len);
		}
		System.out.println(new String(bos.toByteArray()));
	}
}

 

下面的代码工具类转载自http://blog.csdn.net/u012878380/article/details/54907246

 

package Http.client;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 类说明
 * 
 * @author rfk
 */
public class HttpClientUtil {

	public static String doGet(String url, Map<String, String> param) {
		// 创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			// 创建uri
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key));
				}
			}
			URI uri = builder.build();
			// 创建http GET请求
			HttpGet httpGet = new HttpGet(uri);
			// 执行请求
			response = httpclient.execute(httpGet);
			// 判断返回状态是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
				resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null) {
					response.close();
				}
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}

	public static String doGet(String url) {
		return doGet(url, null);
	}

	public static String doPost(String url, Map<String, String> param) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			// 创建参数列表
			if (param != null) {
				List<NameValuePair> paramList = new ArrayList<>();
				for (String key : param.keySet()) {
					paramList.add(new BasicNameValuePair(key, param.get(key)));
				}
				// 模拟表单
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
				httpPost.setEntity(entity);
			}
			// 执行http请求
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}
	
	public static String doPost(String url) {
		return doPost(url, null);
	}

	public static String doPostJson(String url, String json) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			// 创建请求内容
			StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
			httpPost.setEntity(entity);
			// 执行http请求
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}
}

 

 代码上传到github:https://github.com/sona0402/url-httpclient.git

分享到:
评论

相关推荐

    java使用HttpClient通过url下载文件到本地

    Eclipse下完整的java程序,包含HttpClient的全部jar包。通过java类文件,实现通过链接将文件下载本地

    JAVA httpclient jar下载

    httpclient常用封装工具 doGet(String url, Map, String&gt; param) doPost(String url, Map, String&gt; param) doPostJson(String url, String json)

    Java后端HttpClient Post提交文件流 及服务端接收文件流

    HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。

    java.net.URLConnection发送HTTP请求与通过Apache HttpClient发送HTTP请求比较

    NULL 博文链接:https://bijian1013.iteye.com/blog/2299764

    Java Http工具类HttpClientUtil

    多年积累,功能比较强大,可设置路由连接数,时间,请求类型包括get,post, 参数包括urlcode,map,json,xml。注释很清楚。

    java后台访问url需要的包——httpclient方式

    java后台访问url需要的包,没时间好好整理,包可能多几个

    httpClient

    HttpClient httpClient = new HttpClient(); // 设置 Http 连接超时为5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); /* 2 生成 GetMethod 对象并设置参数 */ GetMethod ...

    使用HttpClient发送POST请求,并获取响应内容(附详细步骤).txt

    具体来说,它创建了一个HttpClient实例和一个HttpPost对象,设置了请求的URL、请求头和请求体,然后执行请求并获取响应。最后,它将响应内容输出到控制台。 这个代码的意义在于展示了如何使用Java中的HttpClient库...

    Java使用HttpClient和HtmlParser实现的爬虫Demo.zip

    URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的...

    使用HttpClient下载图片

    HttpURLConnection与HttpClient的区别: HttpClient是个很不错的...HttpURLConnection是java的标准类,可以实现简单的基于URL请求、响应功能,什么都没封装,用起来太原始,比如重访问的自定义,以及一些高级功能等。

    httpclient实例

    实现调用远程servlet方法的实例。有详细注释。和依赖jar包。 //servlet路径 String url = ... result = new String(mc.client(url,params,pageSzie,pageNum));

    HttpClient以及获取页面内容应用

    一般而言,使用HttpClient均需导入httpclient.jar与httpclient-core.jar2个包。 1.4使用方法与步骤 开发环境:需要 使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。 1.创建HttpClient对象。 ...

    java实现上传网络图片到微信临时素材

    主要为大家详细介绍了java实现上传网络图片到微信临时素材,网络图片上传到微信服务器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    用HttpClient来模拟浏览器GET POST

    &lt;br&gt;Commons-httpclient项目就是专门设计来简化HTTP客户端与服务器进行各种通讯编程。通过它可以让原来很头疼的事情现在轻松的解决,例如你不再管是HTTP或者HTTPS的通讯方式,告诉它你想使用HTTPS方式,剩下的...

    java下载网络图片到本地保存

    java下载网络图片到本地保存,还有一个配置文件,用来配置url和保存地址。

    Java抓取网页内容三种方式

    本文将介绍使用 Java 语言抓取网页内容的三种方式:使用 URL 连接、使用 HttpURLConnection 和使用 Apache HttpClient。 第一种方式:使用 URL 连接 使用 URL 连接是最简单的抓取网页内容的方式。它使用 java.net....

    Jsoup+httpclient 模拟登陆和抓取

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient....

    JAVA 人人网登陆实例代码(基于Apache httpclient 4.2.X)

    基于apache httpclient 4.2.X开发 自动处理redirect url jsoup解析response text log4j 自动生成日志 源代码基于UTF-8编码,如果出现乱码请切换到此编码 压缩包 包含所有jar文件。

    urlping:用于 Ping URL 的 Java 实用程序

    URLPing 是一个简单的 Java 实用程序,用于 ping URL。 ping 返回状态为 OKAY、ERROR 或 TIMEOUT。 该实用程序使用 Apache Jakarta Commons HttpClient 组件。 下面介绍URLPing的要求和用法。 要求 Java SE - ...

Global site tag (gtag.js) - Google Analytics