手工打造 HTTP Client

作品

書籍

課程

程式集

小說集

論文集

散文集

影片集

編輯雜誌

程式人

電子書

JavaScript

計算語言學

微積分

Blender 動畫

C# 語言

系統程式

高等 C 語言

Java

Android

Verilog

Wikidot

R 統計軟體

機率統計

計算機數學

組合語言

人工智慧

開放原始碼

網路資源運用

計算機結構

相關訊息

常用工具

友站連結

在家教育

RSS

最新修改

網頁列表

簡體版

English

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
 
class HttpClient {
  static String proxyHost=null;
  static int    proxyPort;
 
  public static void main(String args[]) throws Exception {
    setProxy("proxy.internal", 3128);
//    String url = "http://www.google.com.tw/search?hl=zh-TW&q=abc&meta=";
//    String url = "http://www.google.com.tw/search?as_q=abc&num=100&hl=zh-TW&btnG=Google+%E6%90%9C%E5%B0%8B&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=pdf&as_qdr=all&as_occt=any&as_dt=i&as_sitesearch=&as_rights=&safe=images";
    String url = "http://ccc.kmit.edu.tw/";
    byte[] page = url2bytes(url);
    String pageText = page2text(page);
    bytes2file(page, "HttpClient.txt");
  }
 
  public static void bytes2file(byte[] buf, String outFile) throws Exception {
    FileOutputStream fos = new FileOutputStream(outFile);
    fos.write(buf);
    fos.close();
  }
 
  public static String innerText(String pText, String beginMark, String endMark) {
    int beginStart = pText.indexOf(beginMark);
    if (beginStart < 0) return null;
    int beginEnd = beginStart+beginMark.length();
    int endStart = pText.indexOf(endMark, beginEnd);
    if (endStart < 0) return null;
    return pText.substring(beginEnd, endStart);
  }
 
  public static void setProxy(String pProxy, int pPort) {
      proxyHost = pProxy;
      proxyPort = pPort;
  }
 
  public static byte[] url2bytes(String pUrl) {
    try {
      System.out.println("parse url");
      URL    url = new URL(pUrl);
      String host = url.getHost();
      int    port = url.getPort();
      String protocol = url.getProtocol();
      if (port < 0 && protocol.toLowerCase().equals("http"))
        port = 80;
      String path = url.getPath();
      String query = url.getQuery();
      if (query == null) query = "";
 
      String pathQuery = path;      
      if (!query.equals("")) pathQuery = path+"?"+query;
 
      System.out.println("new socket : host="+host+" port="+port);
      Socket serverSocket;
      if (proxyHost != null) 
          serverSocket = new Socket(proxyHost, proxyPort);
      else
        serverSocket = new Socket(host, port);
 
      System.out.println("connect");
      OutputStream out = serverSocket.getOutputStream();
      InputStream in = serverSocket.getInputStream();
      ByteArrayOutputStream inBuffer = new ByteArrayOutputStream();
      String request =     "GET "+pUrl+" HTTP/1.0\n"+
                        "Accept: */*\n"+
                        "Accept-Language: zh-tw\n"+
                        "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MathPlayer 2.0; .NET CLR 1.1.4322)\n"+
                        "Host: "+host+"\n"+
                        "Proxy-Connection: Keep-Alive\n"+
                        "\n";
      System.out.println("===================request===============\r\n"+request);
      out.write(request.getBytes("UTF-8"));
 
      System.out.println("receive message");
        while (true) {
            byte[] buf = new byte[4096];
            int len = in.read(buf);
//            System.out.println("len="+len);
            if (len < 0) break;
            inBuffer.write(buf, 0, len);
        }
      in.close();
        out.close();
        byte[] response = inBuffer.toByteArray();
        return response;
      } catch (Exception e) {}
      return null;
  }
 
  static String page2text(byte[] page) throws Exception {
    System.out.println("page.length="+page.length);
    String pageText = new String(page, "UTF-8");
    String encode = innerText(pageText, "charset=", "\n");
    if (encode==null) encode = "UTF-8";
    encode = encode.trim();
    pageText = new String(page, encode);
    System.out.println("==============page===================\n"+pageText);
    return pageText;
  }
}

Facebook

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