java httpurlconnection 发送cookie时,cookie要在Post前发送



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 1(key connection.getHeaderFieldKey(i)) != nulli++  
  10.                     if (key.equalsIgnoreCase(“set-cookie”))  
  11.                         cookieVal connection.getHeaderField(i);  
  12.                         cookieVal cookieVal.substring(0cookieVal.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

JDK中的URLConnection参数详解



针对JDK中的URLConnection连接Servlet的问题,网上有虽然有所涉及,但是只是说明了某一个或几个问题,是以FAQ的方式来解决的,而且比较零散,现在对这个类的使用就本人在项目中的使用经验做如下总结: 

1:> URL请求的类别: 

分为二类,GET与POST请求。二者的区别在于: 

    
a:) get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, 

    
b:) post与get的不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。 

2:> URLConnection的对象问题: 

URLConnection的对象,如下代码示例: 



//
下面的index.jsp由<servlet-mapping>映射到 

// 一个Servlet(com.quantanetwork.getClientDataServlet) 

// 该Servlet的注意点下边会提到 

URL url = new URL(http://localhost:8080/TestHttpURLConnectionPro/index.jsp); 



URLConnection rulConnection 
= url.openConnection();// 此处的urlConnection对象实际上是根据URL的 

          
// 请求协议(此处是http)生成的URLConnection类 

          
// 的子类HttpURLConnection,故此处最好将其转化 

          
// 为HttpURLConnection类型的对象,以便用到 

          
// HttpURLConnection更多的API.如下: 



HttpURLConnection httpUrlConnection 
= (HttpURLConnection) rulConnection; 





3:> HttpURLConnection对象参数问题 

// 设置是否向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-typeapplication/x-java-serialized-object); 



// 设定请求的方法为”POST”,默认是GET 

httpUrlConnection.setRequestMethod(POST); 



// 连接,从上述第2条中url.openConnection()至此的配置必须要在connect之前完成, 

        httpUrlConnection.connect(); 





4:> 
HttpURLConnection连接问题: 


// 

此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法, 

// 所以在开发中不调用上述的connect()也可以)。 

OutputStream outStrm = httpUrlConnection.getOutputStream();