Java-Http
Posted 为你编程
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-Http相关的知识,希望对你有一定的参考价值。
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.net.MalformedURLException; 5 import java.net.URL; 6 import java.net.URLConnection; 7 8 /** 9 * URLConnection
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 import java.io.OutputStreamWriter; 6 import java.net.HttpURLConnection; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 10 /** 11 * HttpURLConnection 12 */ 13 public class TestPost { 14 public static void main(String[] args) { 15 try { 16 URL url = new URL(""); 17 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 18 connection.setRequestProperty("encoding", "UTF-8"); 19 connection.setRequestMethod("POST"); 20 connection.setDoInput(true); 21 connection.setDoOutput(true); 22 //根据连接对象创建输出流 将参数写入 23 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); 24 bw.write("name=zhangsan&age=25"); 25 bw.flush(); 26 //创建输入流 获取响应数据 27 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 28 String str; 29 StringBuffer sb = new StringBuffer(); 30 while((str = br.readLine())!=null) { 31 sb.append(str); 32 } 33 bw.close(); 34 br.close(); 35 } catch (MalformedURLException e) { 36 e.printStackTrace(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 }
10 */ 11 public class TestGet { 12 public static void main(String[] args) { 13 try { 14 URL url = new URL("https://www.baidu.com"); 15 //打开连接 16 URLConnection openConnection = url.openConnection(); 17 //获取连接中的输入流openConnection.getInputStream()返回字节流,通过转成字符流并包装一个缓冲流 18 BufferedReader br = new BufferedReader( 19 new InputStreamReader(openConnection.getInputStream())); 20 String str; 21 StringBuffer sb = new StringBuffer(); 22 while((str = br.readLine())!=null) { 23 sb.append(str); 24 } 25 System.out.println(sb); 26 br.close(); 27 } catch (MalformedURLException e) { 28 e.printStackTrace(); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 } 33 }
1 import java.io.IOException; 2 import org.apache.http.HttpEntity; 3 import org.apache.http.HttpResponse; 4 import org.apache.http.client.ClientProtocolException; 5 import org.apache.http.client.HttpClient; 6 import org.apache.http.client.methods.HttpGet; 7 import org.apache.http.impl.client.HttpClients; 8 import org.apache.http.util.EntityUtils; 9 10 /** 11 * HttpGet 12 */ 13 public class HttpClientGetTest { 14 public static void main(String[] args) { 15 HttpClient client = HttpClients.createDefault(); 16 try { 17 HttpGet get = new HttpGet("http://www.baidu.com"); 18 HttpResponse response = client.execute(get); 19 HttpEntity entity = response.getEntity(); 20 String result = EntityUtils.toString(entity,"UTF-8"); 21 22 System.out.println(result); 23 } catch (ClientProtocolException e) { 24 e.printStackTrace(); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 }
1 import java.io.IOException; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 import org.apache.http.HttpEntity; 6 import org.apache.http.HttpResponse; 7 import org.apache.http.client.ClientProtocolException; 8 import org.apache.http.client.HttpClient; 9 import org.apache.http.client.entity.UrlEncodedFormEntity; 10 import org.apache.http.client.methods.HttpPost; 11 import org.apache.http.impl.client.HttpClients; 12 import org.apache.http.message.BasicNameValuePair; 13 import org.apache.http.util.EntityUtils; 14 15 /** 16 * HttpPost 17 */ 18 public class HttpClientPostTest { 19 public static void main(String[] args) { 20 try { 21 HttpClient client = HttpClients.createDefault(); 22 HttpPost post = new HttpPost("http://www.baidu.com"); 23 //参数集合 24 List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); 25 //添加数据 26 parameters.add(new BasicNameValuePair("name", "zhangsan")); 27 //将参数放到实体中 28 post.setEntity(new UrlEncodedFormEntity(parameters)); 29 //设置请求方式 30 HttpResponse response = client.execute(post); 31 HttpEntity entity = response.getEntity(); 32 String result = EntityUtils.toString(entity,"UTF-8"); 33 //打印响应信息 34 System.out.println(result); 35 } catch (ClientProtocolException e) { 36 e.printStackTrace(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 }
以上是关于Java-Http的主要内容,如果未能解决你的问题,请参考以下文章