Beispiel #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;
 }
Beispiel #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();
    }
  }
Beispiel #3
0
  public static void main(String[] args) throws IOException {
    URLConnectionTest u = new URLConnectionTest();

    String urlName = "http://localhost/~huahan/PPServer/index.php/blog/index";
    URL url = new URL(urlName);
    URLConnection connection = url.openConnection();
    connection.connect();

    for (int i = 0; i < 10; i++) u.fun(connection);
  }
  private void sendFile(InputStream in, URLConnection conn) throws IOException {
    conn.connect();

    OutputStream out = conn.getOutputStream();
    try {
      StreamUtils.copyStream(in, out, IO_BUFFER_SIZE);

    } finally {
      out.close();
    }
  }
Beispiel #5
0
  private TrackerInfo doRequest(
      String announce,
      String infoHash,
      String peerID,
      long uploaded,
      long downloaded,
      long left,
      String event)
      throws IOException {
    String s =
        announce
            + "?info_hash="
            + infoHash
            + "&peer_id="
            + peerID
            + "&port="
            + port
            + "&uploaded="
            + uploaded
            + "&downloaded="
            + downloaded
            + "&left="
            + left
            + ((event != NO_EVENT) ? ("&event=" + event) : "");
    URL u = new URL(s);
    if (Snark.debug >= Snark.INFO) Snark.debug("Sending TrackerClient request: " + u, Snark.INFO);

    URLConnection c = u.openConnection();
    c.connect();
    InputStream in = c.getInputStream();

    if (c instanceof HttpURLConnection) {
      // Check whether the page exists
      int code = ((HttpURLConnection) c).getResponseCode();
      if (code / 100 != 2) // We can only handle 200 OK responses
      throw new IOException(
            "Loading '" + s + "' gave error code " + code + ", it probably doesn't exists");
    }

    TrackerInfo info = new TrackerInfo(in, coordinator.getID(), coordinator.getMetaInfo());
    if (Snark.debug >= Snark.INFO) Snark.debug("TrackerClient response: " + info, Snark.INFO);
    lastRequestTime = System.currentTimeMillis();

    String failure = info.getFailureReason();
    if (failure != null) throw new IOException(failure);

    interval = info.getInterval() * 1000;
    return info;
  }