程式作品

C 語言

Java

C#

JavaScript

常用函數

文字處理

遊戲程式

衛星定位

系統程式

資料結構

網路程式

自然語言

人工智慧

機率統計

資訊安全

等待完成

訊息

相關網站

參考文獻

最新修改

簡體版

English

Java 的網路函式庫

package ccc;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import java.util.Properties;
 
public class NET {
  public static void main(String[] args) throws Exception {
      setProxy("proxy.internal", "3128");
    String html1 = url2text("http://localhost:8080/");
    String html2 = url2text("http://www.google.com.tw");
    System.out.println(html1);
    System.out.println(html2);
    System.out.println(html2urls(html1));
//    mail("chen_chungchen@pchome.com.tw", "ccc@mail.km.kuas.edu.tw", "Hello ! Mail test !");
  }
 
  public static void setProxy(String pProxy, String pPort) {
    Properties systemSettings = System.getProperties();
    systemSettings.put("proxySet", "true");
    systemSettings.put("proxyHost", pProxy);
    systemSettings.put("proxyPort", pPort);
    System.setProperties(systemSettings);
  }
 
  public static String url2local(String pUrl) throws Exception {
      if (pUrl.indexOf("://")<0) pUrl = "http://"+pUrl;
      String rzText = url2text(pUrl);
      if (rzText.indexOf("<BASE ")<0)
      rzText = STR.replace(rzText, "<head>", "<head><BASE href=\""+pUrl+"\"/>");
    return rzText;
  }
 
  public static String url2text(String pUrl) throws Exception {
    byte[] buffer = url2bytes(pUrl);
    return new String(buffer);
  }
 
  static int MAX_BYTES = 999999;
  static String redirectUrl = null;
  static String contentType = null;
 
  public static byte[] url2bytes(String pUrl) { 
    if (pUrl == null) return null;
    if (pUrl.indexOf("://")<0) pUrl = "http://"+pUrl;
    byte[] buffer=new byte[MAX_BYTES];
    int bufIdx = 0;
    try {
    URL url=new URL(pUrl);
    URLConnection uc = url.openConnection();
    uc.connect();
    InputStream in = uc.getInputStream();
    for (bufIdx=0; bufIdx < MAX_BYTES ; bufIdx++) {
      int data = in.read();
      if (data < 0) break;
      buffer[bufIdx] = (byte) data;
    }
    in.close();
    redirectUrl = uc.getURL().toString();
    contentType = uc.getContentType();
    } catch (Exception e) {}
    byte[] rzBuf = new byte[bufIdx];
    System.arraycopy(buffer, 0, rzBuf, 0, bufIdx);
    return rzBuf;
  }
 
  static public String fullUrl(String pFileURL, String pPath) { try { 
    URL fileURL = new URL(pFileURL);
    URL fullURL = new URL(fileURL, pPath);
    return fullURL.toString();
  } catch (Exception e) { return null; } }
 
  static Vector html2urls(String text) {
      HashMap rzMap = new HashMap();
    text = STR.replace(text, "src=", "href=");
    Pattern p = Pattern.compile("href=\"[^\"]+\"");
    Matcher m = p.matcher(text);
    while (m.find()) {
        String url = text.substring(m.start()+6, m.end()-1);
        rzMap.put(url, url);
    }
    p = Pattern.compile("http://[^\"]+\"");
    m = p.matcher(text);
    while (m.find()) {
        String url = text.substring(m.start(), m.end()-1);
        rzMap.put(url, url);
    }    
      Vector rzUrls = new Vector();
    rzUrls.addAll(rzMap.keySet());
    return rzUrls;
  }
 
  public static void mail(String pSender, String pReceiver, String pText) throws Exception {
    String[] splits = pReceiver.split("@");
    String server = splits[1];
    Socket socket = new Socket(server, 25);
    InputStreamReader reader = new InputStreamReader(socket.getInputStream());
    BufferedReader in = new BufferedReader(reader);
    OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
    BufferedWriter out = new BufferedWriter(writer);
    mailMsg(in, out, "HELO Joe");
    mailMsg(in, out, "MAIL FROM: <"+pSender+">");
    mailMsg(in, out, "RCPT TO: "+pReceiver);
    mailMsg(in, out, "DATA");
    mailMsg(in, out, pText+"\n.\nQUIT");
  }
 
  public static void mailMsg(BufferedReader in, BufferedWriter out, String pMsg) throws Exception {
    out.write(pMsg+"\n"); out.flush(); in.readLine();    
  }
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License