HttpURLConnection解析

请求响应流程

请求响应流程

设置连接参数的方法

  • setAllowUserInteraction
  • setDoInput
  • setDoOutput
  • setIfModifiedSince
  • setUseCaches
  • setDefaultAllowUserInteraction
  • setDefaultUseCaches

设置请求头或响应头

HTTP请求允许一个key带多个用逗号分开的values,但是HttpURLConnection只提供了单个操作的方法:

  • setRequestProperty(key,value)
  • addRequestProperty(key,value)

setRequestProperty和addRequestProperty的区别就是,setRequestProperty会覆盖已经存在的key的所有values,有清零重新赋值的作用。而addRequestProperty则是在原来key的基础上继续添加其他value。

发送URL请求

建立实际连接之后,就是发送请求,把请求参数传到服务器,这就需要使用outputStream把请求参数传给服务器:

  • getOutputStream 

获取响应

请求发送成功之后,即可获取响应的状态码,如果成功既可以读取响应中的数据,获取这些数据的方法包括:

  • getContent
  • getHeaderField
  • getInputStream 

对于大部分请求来说,getInputStream和getContent是用的最多的。

相应的信息头用以下方法获取:

  • getContentEncoding
  • getContentLength
  • getContentType
  • getDate
  • getExpiration
  • getLastModifed 

HttpURLConnection

任何网络连接都需要经过socket才能连接,HttpURLConnection不需要设置socket,所以,HttpURLConnection并不是底层的连接,而是在底层连接上的一个请求。这就是为什么HttpURLConneciton只是一个抽象类,自身不能被实例化的原因。HttpURLConnection只能通过URL.openConnection()方法创建具体的实例。

虽然底层的网络连接可以被多个HttpURLConnection实例共享,但每一个HttpURLConnection实例只能发送一个请求。请求结束之后,应该调用HttpURLConnection实例的InputStream或OutputStream的close()方法以释放请求的网络资源,不过这种方式对于持久化连接没用。对于持久化连接,得用disconnect()方法关闭底层连接的socket。

创建HttpURLConnection

URL url = new URL("http://localhost:8080/xxx.do");  
   
URLConnection rulConnection = url.openConnection();// 此处的urlConnection对象实际上是根据URL的  
// 请求协议(此处是http)生成的URLConnection类  
// 的子类HttpURLConnection,故此处最好将其转化  
// 为HttpURLConnection类型的对象,以便用到  
// HttpURLConnection更多的API.如下:  
   
HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;  

设置HttpURLConnection参数

// 设定请求的方法为"POST",默认是GET  
httpUrlConnection.setRequestMethod("POST");  
  
// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在  
// http正文内,因此需要设为true, 默认情况下是false;  
httpUrlConnection.setDoOutput(true);  
   
// 设置是否从httpUrlConnection读入,默认情况下是true;  
httpUrlConnection.setDoInput(true);  
   
// Post 请求不能使用缓存  
httpUrlConnection.setUseCaches(false);  
   
// 设定传送的内容类型是可序列化的java对象  
// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)  
httpUrlConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");  
 
// 连接,从上述url.openConnection()至此的配置必须要在connect之前完成,  
httpUrlConnection.connect();  

URLConnection建立连接

// 此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,  
// 所以在开发中不调用上述的connect()也可以)。  
OutputStream outStrm = httpUrlConnection.getOutputStream();  
<p>getInputStream()也是同理。</p>

HttpURLConnection发送请求

// 现在通过输出流对象构建对象输出流对象,以实现输出可序列化的对象。  
ObjectOutputStream objOutputStrm = new ObjectOutputStream(outStrm);  
   
// 向对象输出流写出数据,这些数据将存到内存缓冲区中  
objOutputStrm.writeObject(new String("我是测试数据"));  
   
// 刷新对象输出流,将任何字节都写入潜在的流中(些处为ObjectOutputStream)  
objOutputStm.flush();  
  
// 关闭流对象。此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中,  
// 在调用下边的getInputStream()函数时才把准备好的http请求正式发送到服务器  
objOutputStm.close();  

HttpURLConnection: Post  发送给服务器后台 订单信息, 后台返回  能够支付的真实订单信息 

 1     public  class  getChannelResult implements Callable<String>{
 2       //  private String urlServer = "https://pay.openspeech.cn/api/order_submit";
 3         private String urlServer = "http://test.xfinfr.com/drippay/api/order_submit";
 4         private String payChannel;
 5 
 6         public getChannelResult(String channel){
 7             payChannel = channel;
 8         }
 9 
10         @Override
11         public String call() throws Exception {
12 
13             //初始化订单信息,获得和后台交互的 json串
14             OrderSubmit.Builder builder = new OrderSubmit.Builder();
15             builder.setAmount(1);
16             builder.setChannel(payChannel);
17             OrderSubmit orderSubmit = null;
18             try {
19                 orderSubmit = builder.build();
20             } catch (Exception e) {
21                 e.printStackTrace();
22             }
23             JSONObject json = orderSubmit.toJson();
24 
25             //建立网络连接,得到后台响应的订单信息
26             HttpURLConnection urlConnection = null;
27             StringBuilder response = new StringBuilder();
28             BufferedReader reader = null;
29             try {
30                 URL url = new URL(urlServer);
31                 urlConnection = (HttpURLConnection) url.openConnection();
32                 urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
33                 urlConnection.setRequestProperty("Accept", "application/json");
34                 urlConnection.setConnectTimeout(15000);
35                 urlConnection.setRequestMethod("POST");
36                 urlConnection.setDoInput(true);
37                 urlConnection.setDoOutput(true);
38                 urlConnection.setUseCaches(false);
39                 urlConnection.connect();
40 
41                 DataOutputStream dataJson = new DataOutputStream(urlConnection.getOutputStream());
42                 dataJson.write(json.toString().getBytes());
43                 dataJson.flush();
44                 dataJson.close();
45 
46                 int responseCode = urlConnection.getResponseCode();
47                 Log.d(TAG, "call: "+responseCode);
48             //    if (200 == responseCode) {
49                     InputStream in = urlConnection.getInputStream();
50                     reader = new BufferedReader(new InputStreamReader(in));
51                     String line;
52                     while ((line = reader.readLine()) != null) {
53                         response.append(line);
54                     }
55              //   }
56 
57             } catch (Exception e) {
58                 e.printStackTrace();
59             } finally {
60                 if (null != reader) {
61                     try {
62                         reader.close();
63                     } catch (IOException e) {
64                         e.printStackTrace();
65                     }
66                 }
67                 if (urlConnection != null) {
68                     urlConnection.disconnect();
69                 }
70             }
71             return response.toString();
72         }
73     }
View Code

参考: 

  http://blog.csdn.net/woxueliuyun/article/details/43267365

原文地址:https://www.cnblogs.com/NeilZhang/p/7018107.html