java httpurlconnection 發送cookie時,cookie要在Post前發送

來源:互聯網
上載者:User


Java代碼 
  1. public
     InputStream getStream(URL url,String post,URL cookieurl){  
  2.         HttpURLConnection connection;  
  3.         String cookieVal = null
    ;  
  4.         String sessionId = ""
    ;  
  5.         String key=null
    ;  
  6.         if
    (cookieurl!=
    null
    ){              
  7.             try
    {  
  8.                 connection = (HttpURLConnection)cookieurl.openConnection();  
  9.                 for
     (
    int
     i = 
    1
    ; (key = connection.getHeaderFieldKey(i)) != 
    null
    ; i++ ) {  
  10.                     if
     (key.equalsIgnoreCase(
    "set-cookie"
    )) {  
  11.                         cookieVal = connection.getHeaderField(i);  
  12.                         cookieVal = cookieVal.substring(0
    , cookieVal.indexOf(
    ";"
    ));  
  13.                         sessionId = sessionId+cookieVal+";"
    ;  
  14.                     }  
  15.                 }  
  16.                 InputStream in = connection.getInputStream();  
  17.                 System.out.println(sessionId);  
  18.             }catch
    (MalformedURLException e){  
  19.                 System.out.println("url can't connection"
    );  
  20.                 return
     
    null
    ;  
  21.             }catch
    (IOException e){  
  22.                 System.out.println(e.getMessage());  
  23.                 return
     
    null
    ;  
  24.             }  
  25.         }  
  26.   
  27.         try
     {  
  28.             connection = (HttpURLConnection)url.openConnection();  
  29.             //這個要寫在Post前,否則會取不到值,原因我不知道
      
  30.             if
    (cookieurl!=
    null
    ){  
  31.                 connection.setRequestProperty("Cookie"
    , sessionId);  
  32.             }  
  33.             if
    (post!=
    ""
    ){  
  34.                 connection.setDoOutput(true
    );  
  35.                 connection.setRequestMethod("POST"
    );  
  36.                 connection.getOutputStream().write(post.getBytes());  
  37.                 connection.getOutputStream().flush();  
  38.                 connection.getOutputStream().close();  
  39.             }  
  40.             int
     responseCode = connection.getResponseCode();  
  41.             int
     contentLength = connection.getContentLength();  
  42.             // System.out.println("Content length: "+contentLength);
      
  43.             if
     (responseCode != HttpURLConnection.HTTP_OK ) 
    return
    (
    null
    );  
  44.             InputStream in = connection.getInputStream();  
  45.             return
    (in);  
  46.         }  
  47.         catch
    (Exception e) {  
  48.             // System.out.println(e);
      
  49.             // e.printStackTrace();
      
  50.             return
    (
    null
    );  
  51.         }  
  52.           
  53.     }  

轉自:http://stephenjqj.javaeye.com/blog/477194

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.