HttpURLConnection跨域、跨應用訪問

URL url = new URL("https://www.douban.com/accounts/login");   
        HttpURLConnection httpUrlConnection =(HttpURLConnection)  url.openConnection();  
          
        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");   
 
        // 設定請求的方法爲"POST",默認是GET   
        httpUrlConnection.setRequestMethod("POST");   
          
        httpUrlConnection.setRequestProperty("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-silverlight, */*");  
        httpUrlConnection.setRequestProperty("Referer","http://www.douban.com/accounts/login");  
        httpUrlConnection.setRequestProperty("Accept-Language","zh-cn");  
        httpUrlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
        httpUrlConnection.setRequestProperty("Accept-Encoding","gzip, deflate");  
        httpUrlConnection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; CIBA)");  
        httpUrlConnection.setRequestProperty("Host","www.douban.com");  
        httpUrlConnection.setRequestProperty("Content-Length","139");  
        httpUrlConnection.setRequestProperty("Connection","Keep-Alive");  
        httpUrlConnection.setRequestProperty("Cache-Control","no-cache");  
          
         httpUrlConnection.connect();   
         OutputStream outStrm = httpUrlConnection.getOutputStream();   
           
//       DataOutputStream out = new DataOutputStream(connection  
//                  .getOutputStream());  
//          // The URL-encoded contend  
//          // 正文,正文內容其實跟get的URL中'?'後的參數字符串一致  
//          String content = "firstname=" + URLEncoder.encode("一個大肥人", "utf-8");  
//          // DataOutputStream.writeBytes將字符串中的16位的unicode字符以8位的字符形式寫道流裏面  
//          out.writeBytes(content);   
//         
//       //現在通過輸出流對象構建對象輸出流對象,以實現輸出可序列化的對象。   
         String content="source=simple&redir=http%3A%2F%2Fmovie.douban.com%2Fchart&form_email=email&form_password=password&user_login=%E7%99%BB%E5%BD%95";  
         outStrm.write(content.getBytes());  
         outStrm.flush();  
         outStrm.close(); // flush and close  
 
         InputStream inStrm = httpUrlConnection.getInputStream();   
           
         BufferedReader br = new BufferedReader(new InputStreamReader(inStrm,"gbk"));  
         String temp="";  
         while((temp=br.readLine())!=null){  
             System.out.println(temp);  
         }
發佈了8 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章