程式作品C 語言JavaC#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(); } } |
page revision: 2, last edited: 04 Nov 2010 03:33
Post preview:
Close preview