使用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