Android 使用HttpURLConnection实现Http请求

 1 //package com.example.red_butterfly.teacher_vote;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.ByteArrayOutputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.PrintWriter;
 8 import java.net.HttpURLConnection;
 9 import java.net.URL;
10 import java.util.Random;
11 
12 /**
13  * Created by horou_dsk on 2017/8/3.
14  */
15 
16 public class HttpClient {
17 
18     static HttpURLConnection HttpGetUrlConnection = null;
19     static HttpURLConnection HttpPostUrlConnection = null;
20 
21     public static String sendHttpGet(String url){
22         URL _url = null;
23         //打开连接
24         try {
25             _url = new URL(url);
26             HttpGetUrlConnection = (HttpURLConnection) _url.openConnection();
27             //添加请求头 setRequestProperty
28             //HttpGetUrlConnection.setRequestProperty("x-forwarded-for","255.255.255.255");
29             if(200 == HttpGetUrlConnection.getResponseCode()){
30                 //得到输入流
31                 InputStream is =HttpGetUrlConnection.getInputStream();
32                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
33                 byte[] buffer = new byte[1024];
34                 int len = 0;
35                 while(-1 != (len = is.read(buffer))){
36                     baos.write(buffer,0,len);
37                     baos.flush();
38                 }
39                 return baos.toString("utf-8");
40             }
41         } catch (IOException e) {
42             e.printStackTrace();
43         }
44 
45         return "";
46     }
47 
48     public static String sendHttpPost(String url){
49         URL _url = null;
50         try {
51             _url = new URL(url);
52             HttpPostUrlConnection = (HttpURLConnection) _url.openConnection();
53             HttpPostUrlConnection.setRequestMethod("POST");// 提交模式
54             // conn.setConnectTimeout(10000);//连接超时 单位毫秒
55             // conn.setReadTimeout(2000);//读取超时 单位毫秒
56             // 发送POST请求必须设置如下两行
57             HttpPostUrlConnection.setDoOutput(true);
58             HttpPostUrlConnection.setDoInput(true);
59             // 获取URLConnection对象对应的输出流
60             PrintWriter printWriter = new PrintWriter(HttpPostUrlConnection.getOutputStream());
61             // 发送请求参数
62             printWriter.write("");//post的参数 xx=xx&yy=yy
63             // flush输出流的缓冲
64             printWriter.flush();
65             //开始获取数据
66             BufferedInputStream bis = new BufferedInputStream(HttpPostUrlConnection.getInputStream());
67             ByteArrayOutputStream bos = new ByteArrayOutputStream();
68             int len;
69             byte[] arr = new byte[1024];
70             while((len=bis.read(arr))!= -1){
71                 bos.write(arr,0,len);
72                 bos.flush();
73             }
74             bos.close();
75             return bos.toString("utf-8");
76         } catch (Exception e) {
77             e.printStackTrace();
78         }
79         return "";
80     }
81 }
原文地址:https://www.cnblogs.com/Red-ButterFly/p/7280987.html