代码之家  ›  专栏  ›  技术社区  ›  Señor Reginold Francis

Android POST数据和COOKIES

  •  0
  • Señor Reginold Francis  · 技术社区  · 16 年前

    我该怎么做?

    谢谢

    2 回复  |  直到 16 年前
        1
  •  1
  •   Maz    16 年前

    你可以打电话给 setCookie function

        2
  •  1
  •   Yar    15 年前

    通常,在Java HttpURLConnection中,您可以这样设置/获取cookie(以下是整个连接过程):

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();             
    
                //set cookie. sCookie is my static cookie string
                if(sCookie!=null && sCookie.length()>0){
                    conn.setRequestProperty("Cookie", sCookie);                  
                }
    
                // Send data
                OutputStream os = conn.getOutputStream(); 
                os.write(mData.getBytes());
                os.flush();
                os.close(); 
    
                // Get the response!
                int httpResponseCode = conn.getResponseCode();         
                if (httpResponseCode != HttpURLConnection.HTTP_OK){
                   throw new Exception("HTTP response code: "+httpResponseCode); 
                }
    
                // Get the data and pass them to the XML parser
                InputStream inputStream = conn.getInputStream();                
                Xml.parse(inputStream, Xml.Encoding.UTF_8, mSaxHandler);                
                inputStream.close();
    
                //Get the cookie
                String cookie = conn.getHeaderField("set-cookie");
                if(cookie!=null && cookie.length()>0){
                    sCookie = cookie;              
                }
    
                /*   many cookies handling:                  
                String responseHeaderName = null;
                for (int i=1; (responseHeaderName = conn.getHeaderFieldKey(i))!=null; i++) {
                    if (responseHeaderName.equals("Set-Cookie")) {                  
                    String cookie = conn.getHeaderField(i);   
                    }
                }*/                
    
                conn.disconnect();