Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import java.net.*;
  2. import java.security.cert.Certificate;
  3. import java.io.*;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.HashMap;
  7.  
  8. public class HttpFetcher {
  9.     /**
  10.      * @param args
  11.      */
  12.     public static void main(String[] args) {
  13.         // TODO Auto-generated method stub
  14.         HttpFetcher http = new HttpFetcher();
  15.         String postData = "user=user1234&password=pass1234";
  16.         String html = "";
  17.        
  18.         //Build request headers
  19.         RequestHeaders request = new RequestHeaders();
  20.         request.set("Content-Type", "application/x-www-form-urlencoded");
  21.         request.set("Content-Length", "" + Integer.toString(postData.getBytes().length));
  22.         request.set("Content-Language", "en-US");
  23.         Map<String,List<String>> responseHeaders = new HashMap<String,List<String>>();
  24.         try{
  25.             html = http.executePost("http://foobar.com",postData,request);
  26.             System.out.println(responseHeaders.size());
  27.             if( responseHeaders.containsKey("Set-Cookie")){
  28.                 System.out.println("Contains cookie");
  29.             }else{
  30.                 System.out.println("Doesn't contain cookie");
  31.             }
  32.            
  33.            
  34.         }catch(IOException e){
  35.             System.out.println("IOException");
  36.         }
  37.         //System.out.println(html);
  38.     }
  39.  
  40.  
  41.    
  42.     public static String executePost(String targetURL, String urlParameters,RequestHeaders request) throws IOException {
  43.         URL url;
  44.         HttpURLConnection connection = null;  
  45.         try {
  46.             //Create connection
  47.             url = new URL(targetURL);
  48.             connection = (HttpURLConnection)url.openConnection();
  49.             connection.setRequestMethod("POST");
  50.             //Apply request headers
  51.             request.apply(connection);
  52.             connection.setUseCaches (false);
  53.             connection.setDoInput(true);
  54.             connection.setDoOutput(true);
  55.  
  56.             //Send request
  57.             DataOutputStream wr = new DataOutputStream (
  58.                     connection.getOutputStream ());
  59.             wr.writeBytes (urlParameters);
  60.             wr.flush ();
  61.             wr.close ();
  62.  
  63.             //Get Response    
  64.             InputStream is = connection.getInputStream();
  65.             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  66.             String line;
  67.             StringBuffer response = new StringBuffer();
  68.             while((line = rd.readLine()) != null) {
  69.                 response.append(line);
  70.                 response.append('\r');
  71.             }
  72.             rd.close();
  73.             //Response headers
  74.             responseHeaders = connection.getHeaderFields();
  75.             //System.out.println( responseHeaders.toString() );
  76.             return response.toString();
  77.  
  78.         } catch (Exception e) {
  79.  
  80.           e.printStackTrace();
  81.           return null;
  82.  
  83.         } finally {
  84.  
  85.           if(connection != null) {
  86.             connection.disconnect();
  87.           }
  88.         }
  89.     }
  90.  
  91.  
  92.  
  93.     private static String get_content(HttpURLConnection con){
  94.         String content = "";
  95.         if(con!=null){
  96.             try {
  97.                 BufferedReader br =
  98.                         new BufferedReader(
  99.                                 new InputStreamReader(con.getInputStream()));
  100.                 String input;
  101.                 while ((input = br.readLine()) != null){
  102.                     content += input;
  103.                 }
  104.                 br.close();
  105.             } catch (IOException e) {
  106.                 e.printStackTrace();
  107.             }
  108.         }
  109.         return content;
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement