예제 #1
0
 public static String sendGet(String url, String params) {
   String result = "";
   BufferedReader in = null;
   try {
     String urlName = url + "?" + params;
     URL realUrl = new URL(urlName);
     URLConnection conn = realUrl.openConnection();
     conn.setRequestProperty("accept", "*/*");
     conn.setRequestProperty("connection", "Keep-Alive");
     conn.setRequestProperty(
         "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
     conn.connect();
     Map<String, List<String>> map = conn.getHeaderFields();
     for (String key : map.keySet()) {
       System.out.println(key + "--->" + map.get(key));
     }
     in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
     String line;
     while ((line = in.readLine()) != null) {
       result += "\n" + line;
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     try {
       if (in != null) {
         in.close();
       }
     } catch (IOException ex) {
       ex.printStackTrace();
     }
   }
   return result;
 }
예제 #2
0
  public void fun(URLConnection connection) {
    try {
      connection.connect();
      // print header fields

      Map<String, List<String>> headers = connection.getHeaderFields();
      for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        String key = entry.getKey();
        for (String value : entry.getValue()) System.out.println(key + ": " + value);
      }

      // print convenience functions

      System.out.println("----------");
      System.out.println("getContentType: " + connection.getContentType());
      System.out.println("getContentLength: " + connection.getContentLength());
      System.out.println("getContentEncoding: " + connection.getContentEncoding());
      System.out.println("getDate: " + connection.getDate());
      System.out.println("getExpiration: " + connection.getExpiration());
      System.out.println("getLastModifed: " + connection.getLastModified());
      System.out.println("----------");

      Scanner in = new Scanner(connection.getInputStream());

      // print first ten lines of contents

      for (int n = 1; in.hasNextLine() && n <= 10; n++) System.out.println(in.nextLine());
      if (in.hasNextLine()) System.out.println(". . .");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
예제 #3
0
  // getConnectionInfiniteCookie
  public static String getConnectionInfiniteCookie(URLConnection urlConnection) {
    Map<String, List<String>> headers = urlConnection.getHeaderFields();
    Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet();

    for (Map.Entry<String, List<String>> entry : entrySet) {
      String headerName = entry.getKey();
      if (headerName != null && headerName.equals("Set-Cookie")) {
        List<String> headerValues = entry.getValue();
        for (String value : headerValues) {
          if (value.contains("infinitecookie")) {
            int equalsLoc = value.indexOf("=");
            int semicolonLoc = value.indexOf(";");
            return value.substring(equalsLoc + 1, semicolonLoc);
          }
        }
      }
    }
    return null;
  } // TESTED