转:java网络编程-HTTP编程
Posted PacosonSWJTU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了转:java网络编程-HTTP编程相关的知识,希望对你有一定的参考价值。
转自:
【1】HTTP编程
Java HTTP编程
支持模拟成浏览器的方式去访问网页
URL, Uniform Resource Locator,代表一个资源
URLConnection
获取资源连接器
根据URL的openConnection()方法获得URLConnection
connect方法,建立和资源的联系通道
getInputStream方法,获取资源的内容
【2】URLConnection
示例代码1:Get获取网页html-使用URLConnection
package com.lihuan.network.demo03;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public class URLConnectionGetTest {
public static void main(String[] args) {
try {
String urlName = "http://www.baidu.com";
URL url = new URL(urlName);
URLConnection connection = url.openConnection();
//建立联系通道
connection.connect();
//打印http的头部信息
Map<String, List<String>> headers = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : headers.entrySet()){
String key = entry.getKey();
for (String value : entry.getValue()){
System.out.println(key + ":" + value);
}
}
//输出将要收到的内容属性信息
System.out.println("-------------");
System.out.println("getContentType: " + connection.getContentType());
System.out.println("getContentLength: " + connection.getContentLength());
System.out.println("getContentEncoding: " + connection.getContentEncoding());
System.out.println("getDate: " + connection.getDate());
System.out.println("getExpiration: " + connection.getExpiration());
System.out.println("getLastModified:" + connection.getLastModified());
System.out.println("-------------");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
// 输出收到的内容
String line = "";
while ((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
实例代码2: Post提交表单-使用 HttpURLConnection
package com.lihuan.network.demo03;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class URLConnectionPostTest {
public static void main(String[] args) throws IOException {
String urlString = "https://tools.usps.com/zip-code-lookup.htm?byaddress";
Object userAgent = "HTTPie/0.9.2";
Object redirects = "1";
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
Map<String, String> params = new HashMap<String, String>();
params.put("tAddress", "1 Market Street");
params.put("tCity", "san Francisco");
params.put("sState", "CA");
String result = doPost(new URL(urlString), params,
userAgent == null ? null : userAgent.toString(),
redirects == null ? -1 : Integer.parseInt(redirects.toString()));
System.out.println(result);
}
public static String doPost(URL url, Map<String, String> nameValuePairs, String userAgent, int redirects) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置请求头
if(userAgent != null){
connection.setRequestProperty("User-Agent", userAgent);
}
//设为不自动重定向
if(redirects >= 0){
connection.setInstanceFollowRedirects(false);
}
//设置可以使用conn.getOutputStream().print
connection.setDoOutput(true);
//输出请求的参数
try (PrintWriter out = new PrintWriter(connection.getOutputStream())){
boolean first = true;
for (Map.Entry<String, String> pair : nameValuePairs.entrySet()){
//参数拼接
if(first){
first = false;
}else{
out.print('&');
}
String name = pair.getKey();
String value = pair.getValue();
out.print(name);
out.print("=");
out.print(URLEncoder.encode(value, "UTF-8"));
}
}
String encoding = connection.getContentEncoding();
if(encoding == null){
encoding = "UTF-8";
}
if(redirects > 0){
int responseCode = connection.getResponseCode();
System.out.println("responseCode: " + responseCode);
if(responseCode == HttpURLConnection.HTTP_MOVED_PERM
|| responseCode == HttpURLConnection.HTTP_MOVED_TEMP
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER){
String location = connection.getHeaderField("Location");
if(location != null){
URL base = connection.getURL();
connection.disconnect();
return doPost(new URL(base, location), nameValuePairs, userAgent, redirects - 1);
}
}
}else if(redirects == 0){
throw new IOException("Too many redirects");
}
//接下来获取html内容
StringBuilder response = new StringBuilder();
try (Scanner in = new Scanner(connection.getInputStream(), encoding)){
while (in.hasNextLine()){
response.append(in.nextLine());
response.append("\\n");
}
}catch (IOException e){
InputStream err = connection.getErrorStream();
if(err == null) throw e;
try (Scanner in = new Scanner(err)){
response.append(in.nextLine());
response.append("\\n");
}
}
return response.toString();
}
}
【3】JDK HttpClient
JDK 9新增,JDK10更新,JDK11正式发
java.net.http包
取代URLConnection
支持HTTP/1.1和HTTP/2
实现大部分HTTP方法
主要类
HttpClient
HttpRequest
HttpResponse
HttpComponents
是一个集成的Java HTTP工具包
实现所有HTTP方法: get/post/put/delete
支持自动转向
支持https协议
支持代理服务器等
HttpComponent示例代码3:Get获取网页html
package com.lihuan.network.demo04;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
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;
import java.io.IOException;
public class HttpComponentGetTest {
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.setRedirectsEnabled(true)
.build();
HttpGet httpGet = new HttpGet("http://www.baidu.com");
httpGet.setConfig(requestConfig);
String strResult = "";
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
strResult = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
System.out.println(strResult);
}else{
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
HttpComponent实例代码4:Post提交表单
package com.lihuan.network.demo04;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
public class HttpComponentsPostTest {
public static void main(String[] args) throws UnsupportedEncodingException {
//获取可关闭的 httpClient
CloseableHttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
//配置超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(10000).setConnectionRequestTimeout(10000)
.setSocketTimeout(10000).setRedirectsEnabled(false).build();
HttpPost httpPost = new HttpPost("https://tools.usps.com/zip-code-lookup.htm?byaddress");
//设置超时时间
httpPost.setConfig(requestConfig);
List<BasicNameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("tAddress", URLEncoder.encode("1 Market Street", "UTF-8")));
list.add(new BasicNameValuePair("tCity", URLEncoder.encode("san Francisco", "UTF-8")));
list.add(new BasicNameValuePair("sState", "CA"));
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
//设置post请求参数
httpPost.setEntity(entity);
httpPost.setHeader("User-Agent", "HTTPie/0.9.2");
HttpResponse httpResponse = httpClient.execute(httpPost);
String strResult = "";
if(httpResponse != null){
System.out.println(httpResponse.getStatusLine().getStatusCode());
if(httpResponse.getStatusLine().getStatusCode() == 200){
strResult = EntityUtils.toString(httpResponse.getEntity());
}else{
strResult = "Error Response:" + httpResponse.getStatusLine().toString();
}
}else{
}
System.out.println(strResult);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
以上是关于转:java网络编程-HTTP编程的主要内容,如果未能解决你的问题,请参考以下文章
转: Java并发编程之二十一:并发新特性—阻塞队列和阻塞栈(含代码)
转:Java并发编程之十一:线程间通信中notify通知的遗漏(含代码)