android http 学习笔记

来源:互联网 发布:网络借贷法律 编辑:程序博客网 时间:2024/05/21 10:49

1.Expect 100 Continue 产生原因

不设置HttpProtocolParams.setUseExpectContinue(params, false); 默认会期待服务端进行两次请求:

源地址:http://blog.csdn.net/androidzhaoxiaogang/article/details/8164637

Expect 100 Continue其实是http协议1.1中的一个header属性, 就是说如果设置了Expect 100 Continue 那么就意味着客户端在向服务器发送数据的时候,需要先向服务器发起一个请求看服务器是否愿意接受客户端将要发送的数据(我们这里可以认为是http body,往往是较大的数据块)。

因为Expect 100 comtinue会导致客户端在向服务器发送数据是进行两次请求,这样对通信的性能方面将会受到一定的影响,但这种情况在做验证或者给予curl的服务器是很常见的,这也是该属性的主要应用场合。


[java] view plaincopy
  1. public static void  [More ...] setUseExpectContinue(final HttpParams params, boolean b) {  
  2.         if (params == null) {  
  3.             throw new IllegalArgumentException("HTTP parameters may not be null");  
  4.         }     params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, b);  
  5. }  

而 public static final String USE_EXPECT_CONTINUE = "http.protocol.expect-continue";

也正是我们上面说的Expect 100 Continue,如果我们在用HttpClient做为网络底层接口时候注意设置HttpProtocolParams.setUseExpectContinue(params, false); 原因我在上面已经说得很清楚了。


2.

ThreadSafeClientConnManager用来支持多线程的使用http client


    import org.apache.http.HttpEntity;      import org.apache.http.HttpResponse;      import org.apache.http.client.HttpClient;      import org.apache.http.client.methods.HttpGet;      import org.apache.http.impl.client.DefaultHttpClient;      import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;      import org.apache.http.protocol.BasicHttpContext;      import org.apache.http.protocol.HttpContext;      import org.apache.http.util.EntityUtils;            /**      * An example that performs GETs from multiple threads.      *      */      public class ClientMultiThreadedExecution {                public static void main(String[] args) throws Exception {              // Create an HttpClient with the ThreadSafeClientConnManager.              // This connection manager must be used if more than one thread will              // be using the HttpClient.              ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();              cm.setMaxTotal(100);                    HttpClient httpclient = new DefaultHttpClient(cm);              try {                  // create an array of URIs to perform GETs on                  String[] urisToGet = {                      "http://hc.apache.org/",                      "http://hc.apache.org/httpcomponents-core-ga/",                      "http://hc.apache.org/httpcomponents-client-ga/",                      "http://svn.apache.org/viewvc/httpcomponents/"                  };                        // create a thread for each URI                  GetThread[] threads = new GetThread[urisToGet.length];                  for (int i = 0; i < threads.length; i++) {                      HttpGet httpget = new HttpGet(urisToGet[i]);                      threads[i] = new GetThread(httpclient, httpget, i + 1);                  }                        // start the threads                  for (int j = 0; j < threads.length; j++) {                      threads[j].start();                  }                        // join the threads                  for (int j = 0; j < threads.length; j++) {                      threads[j].join();                  }                    } finally {                  // When HttpClient instance is no longer needed,                  // shut down the connection manager to ensure                  // immediate deallocation of all system resources                  httpclient.getConnectionManager().shutdown();              }          }                /**          * A thread that performs a GET.          */          static class GetThread extends Thread {                    private final HttpClient httpClient;              private final HttpContext context;              private final HttpGet httpget;              private final int id;                    public GetThread(HttpClient httpClient, HttpGet httpget, int id) {                  this.httpClient = httpClient;                  this.context = new BasicHttpContext();                  this.httpget = httpget;                  this.id = id;              }                    /**              * Executes the GetMethod and prints some status information.              */              @Override              public void run() {                        System.out.println(id + " - about to get something from " + httpget.getURI());                        try {                            // execute the method                      HttpResponse response = httpClient.execute(httpget, context);                            System.out.println(id + " - get executed");                      // get the response body as an array of bytes                      HttpEntity entity = response.getEntity();                      if (entity != null) {                          byte[] bytes = EntityUtils.toByteArray(entity);                          System.out.println(id + " - " + bytes.length + " bytes read");                      }                        } catch (Exception e) {                      httpget.abort();                      System.out.println(id + " - error: " + e);                  }              }                }            }  
3.http://www.360doc.com/content/11/0816/15/2631212_140795867.shtml 

 代理设置

0 0
原创粉丝点击