コード例 #1
0
ファイル: ChannelUtil.java プロジェクト: newsmoke/Channel
  public static String postPage(
      URLConnection connection, String query, String cookie, HashMap<String, String> hdr) {
    URL url = connection.getURL();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setUseCaches(false);
    connection.setDefaultUseCaches(false);
    // connection.setAllowUserInteraction(true);

    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("User-Agent", defAgentString);
    connection.setRequestProperty("Content-Length", "" + query.length());

    try {
      String c1 = ChannelCookie.getCookie(url.toString());
      if (!empty(c1)) {
        if (!cookieContains(c1, cookie)) cookie = append(cookie, "; ", c1);
      }
      if (!empty(cookie)) connection.setRequestProperty("Cookie", cookie);
      if (hdr != null && hdr.size() != 0) {
        for (String key : hdr.keySet()) connection.setRequestProperty(key, hdr.get(key));
      }
      connection.setConnectTimeout(10000);

      connection.connect();
      // open up the output stream of the connection
      if (!empty(query)) {
        DataOutputStream output = new DataOutputStream(connection.getOutputStream());
        output.writeBytes(query);
        output.flush();
        output.close();
      }

      BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      StringBuilder page = new StringBuilder();
      String str;
      while ((str = in.readLine()) != null) {
        //	page.append("\n");
        page.append(str.trim());
        page.append("\n");
      }
      in.close();
      Channels.restoreProxyDNS();
      return page.toString();
    } catch (Exception e) {
      Channels.debug("post error " + e);
      Channels.restoreProxyDNS();
      return "";
    }
  }
コード例 #2
0
ファイル: ChannelUtil.java プロジェクト: newsmoke/Channel
 public static String fetchPage(
     URLConnection connection, ChannelAuth auth, String cookie, HashMap<String, String> hdr) {
   try {
     //			URLConnection connection=url.openConnection();
     URL url = connection.getURL();
     connection.setConnectTimeout(10000);
     connection.setRequestProperty("User-Agent", defAgentString);
     if (auth != null) {
       Channels.debug("auth " + auth.method + " authstr " + auth.authStr);
       if (auth.method == ChannelLogin.STD)
         connection.setRequestProperty("Authorization", auth.authStr);
       else if (auth.method == ChannelLogin.COOKIE) cookie = append(cookie, "; ", auth.authStr);
       else if (auth.method == ChannelLogin.APIKEY) {
         url = new URL(url.toString() + auth.authStr);
         connection = url.openConnection();
       }
     }
     Channels.debug("fpage cookie " + cookie);
     String c1 = ChannelCookie.getCookie(url.toString());
     if (!empty(c1)) {
       if (!cookieContains(c1, cookie)) {
         cookie = append(cookie, "; ", c1);
       }
     }
     Channels.debug("fpage2 cookie " + cookie + " " + url);
     if (!empty(cookie)) connection.setRequestProperty("Cookie", cookie);
     if (hdr != null && hdr.size() != 0) {
       for (String key : hdr.keySet()) connection.setRequestProperty(key, hdr.get(key));
     }
     //	connection.setRequestProperty("Content-Length", "0");
     connection.setDoInput(true);
     connection.setDoOutput(true);
     BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
     StringBuilder page = new StringBuilder();
     String str;
     while ((str = in.readLine()) != null) {
       //	page.append("\n");
       page.append(str.trim());
       page.append("\n");
     }
     in.close();
     Channels.restoreProxyDNS();
     return page.toString();
   } catch (Exception e) {
     Channels.debug("fetch exception " + e.toString());
     Channels.restoreProxyDNS();
     return "";
   }
 }