关于使用纯java获取http url请求返回的信息
话不多说 , 直接上代码 (没有依赖jar)
1 import java.io.ByteArrayOutputStream; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.OutputStream; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 8 9 public class HttpTest { 10 11 12 public static void main(String[] args) { 13 String path = "http://127.0.0.1:8080/demo/test.servlet" ; 14 // String path = "https://www.baidu.com/" ;//只能支持get请求 15 16 HttpURLConnection httpURLConnection = null ; 17 ByteArrayOutputStream byteArrayOutputStream = null ; 18 OutputStream outputStream = null ; 19 InputStream inputStream = null ; 20 URL url = null ; 21 22 //从输出流中获取读取到数据(服务端返回的) 23 String message = null ; 24 try { 25 //请求的url 26 url = new URL(path); 27 28 //创建http链接 29 httpURLConnection = (HttpURLConnection) url.openConnection(); 30 31 //设置超时时间 32 httpURLConnection.setConnectTimeout(5000);// 5 秒 33 httpURLConnection.setReadTimeout(5000); 34 35 //设置请求的方法类型 36 httpURLConnection.setRequestMethod("POST"); 37 38 //设置通用的请求属性 设置请求格式 39 httpURLConnection.setRequestProperty("contentType", "utf-8"); 40 httpURLConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 41 42 //设置发送数据 43 httpURLConnection.setDoOutput(true); 44 //设置接受数据 45 httpURLConnection.setDoInput(true); 46 47 //发送数据,使用输出流 48 outputStream = httpURLConnection.getOutputStream(); 49 outputStream.write(new String("name=zhangshang&pwd=123").getBytes()); 50 outputStream.flush() ; 51 // //发送数据 52 if (httpURLConnection.getResponseCode() == 200) {//成功 53 //接收数据 54 inputStream = httpURLConnection.getInputStream(); 55 //定义字节数组 56 byte[] b = new byte[1024]; 57 //定义一个输出流存储接收到的数据 58 byteArrayOutputStream = new ByteArrayOutputStream(); 59 //开始接收数据 60 int len = 0; 61 while (true) { 62 len = inputStream.read(b); 63 if (len == -1) { 64 //数据读完 65 break; 66 } 67 byteArrayOutputStream.write(b, 0, len); 68 } 69 }else{//失败 70 System.err.println("异常HTTP状态码 : "+httpURLConnection.getResponseCode()); 71 //接收数据 72 inputStream = httpURLConnection.getInputStream(); 73 //定义字节数组 74 byte[] b = new byte[1024]; 75 //定义一个输出流存储接收到的数据 76 byteArrayOutputStream = new ByteArrayOutputStream(); 77 //开始接收数据 78 int len = 0; 79 while (true) { 80 len = inputStream.read(b); 81 if (len == -1) { 82 //数据读完 83 break; 84 } 85 byteArrayOutputStream.write(b, 0, len); 86 } 87 88 } 89 message = byteArrayOutputStream.toString("utf-8"); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } finally{ 93 if(null != byteArrayOutputStream){ 94 try { 95 byteArrayOutputStream.close() ; 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 } 100 if(null != inputStream){ 101 try { 102 inputStream.close() ; 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } 106 } 107 if(null != outputStream){ 108 try { 109 outputStream.close() ; 110 } catch (IOException e) { 111 e.printStackTrace(); 112 } 113 } 114 if(null != httpURLConnection){ 115 httpURLConnection.disconnect() ; 116 } 117 } 118 System.out.println(message); 119 } 120 }
- 打开和url之间的连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); -
设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
conn.setDoOutput(true);
conn.setDoInput(true); - 断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。
conn.disconnect();
发送http请求然后返回的方法还有很多种 , 我知道有一种叫 httpclient 的jar包 , 封装很完善 , 很好使 ...