[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfriends] Re: GETコマンドと404 File Not Found




>  http://www.j-teens.com/pd1J/f1/main.html というURLなのですが、

えんどうです。大変興味深いコンテンツですね。;-   >

>IE や Netscapefはもちろんのこと、JavaのURLクラスを使えば読み込
>めるのは確認しています。しかし、telnet や C++Builder付属のHTTP
>コンポーネント及びHTMLコンポーネントを利用するとなぜか読み込め
>ません。

結城浩先生のSmallHttpProxy.javaを改造して
ログ(というか全送受信バイト)を取れるようにしたので、
何かの足しにしてください。
(コンパイルは通ったけどまだテストしてません)

成功している方のGET内容をダンプして検討してみるとよいかも知れません。

SmallHttpProxy.java
====================================================================
/*
 * SmallHttpProxy.java
 *
 * Copyright (C) 1998 by Hiroshi Yuki.
 * All rights reserved.
 * <hyuki@xxxxxxxxxx>
 * http://www.st.rim.or.jp/~hyuki/
 *
 * $Id: SmallHttpProxy.java 1.1 1998/08/03 08:00:30 yuki Exp $
 */

import java.net.*;
import java.io.*;
import java.util.*;
import jp.co.timedia.util.*;

/**
 *
 * A Small HTTP Proxy.
 * <p>
 * Format: <code>java SmallHttpProxy port-number server-URL</code>
 * </p>
 * <p>
 * SmallHttpProxy is a small HTTP proxy.
 * When an HTTP client is connecting to SmallHttpProxy,
 * SmallHttpProxy redirects the client's request to remote host (server).
 * The URL of remote host is given by the command line.
 * </p>
 *
 * @xxxxxxxxxx Hiroshi Yuki &lt;hyuki@xxxxxxxxxx&gt;
 * @xxxxxxxxxx java.net.ServerSocket
 * @xxxxxxxxxx java.net.Socket
 * @xxxxxxxxxx ProxyThread
 */

public class SmallHttpProxy {
    public static void main(String args[]) {
        if (args.length != 2) {
            System.out.println(
                  "This is SmallHttpProxy, $Revision: 1.1 $\n"
                + "\n"
                + "Usage: java SmallHttpProxy my-port-number server-URL\n"
                + "Example: java SmallHttpProxy 8888 http://www.st.rim.or.jp/~hyuki/\n");
            return;
        }
        SmallHttpProxy self = new SmallHttpProxy();
        self.session(new Integer(args[0]).intValue(), args[1]);
    }
    /**
     * session() creates a ServerSocket and waits for client's connection.
     * When HTTP client has connected to SmallHttpProxy,
     * an instance of ProxyThread is created and started.
     * session() continues waiting for other clients.
     * session() will exit when IOException is thrown.
     *
     * @xxxxxxxxxx portno listen port number.
     * @xxxxxxxxxx serverUrlString server's URL.
     */
    void session(int portno, String serverUrlString) {
        /* create server socket */
        ServerSocket proxySocket;
        URL serverUrl;
        try {
            proxySocket = new ServerSocket(portno);
            System.out.println("Listening on " + proxySocket);
        } catch (IOException e) {
            System.out.println("ServerSocket:"+e);
            return;
        }
        /* create serverUrl */
        try {
            serverUrl = new URL(serverUrlString);
        } catch (MalformedURLException e) {
            System.out.println("URL:"+e);
            return;
        }
        String hostName = serverUrl.getHost();
        int portNumber = serverUrl.getPort();
        if (portNumber < 0) {
            portNumber = 80;
        }
        System.out.println("serverUrl:host = " + hostName);
        System.out.println("serverUrl:port = " + portNumber);

        FileWriter inlog = null;
        FileWriter outlog = null;
        try {
          inlog = new FileWriter("in.log");
          outlog = new FileWriter("out.log");
        } catch (IOException e) { e.printStackTrace(); }
        for (;;) {
            Socket clientSocket;
            Socket serverSocket;
            try {
                clientSocket = proxySocket.accept();
            } catch (IOException e) {
                System.out.println("accept:"+e);
                break;
            }
            System.out.println("Connected to client : " + clientSocket);
            try {
                serverSocket = new Socket(hostName, portNumber);
            } catch (UnknownHostException e) {
                System.out.println("Socket:"+e);
                break;
            } catch (IOException e) {
                System.out.println("Socket:"+e);
                break;
            }
            ProxyThread requester = new ProxyThread(clientSocket, serverSocket);
            requester.addIView(new Log(outlog));
            requester.start();
            ProxyThread responder = new ProxyThread(serverSocket, clientSocket);
            responder.addIView(new Log(inlog));
            responder.start();
        }
        try {
            proxySocket.close();
            inlog.close();
            outlog.close();
        } catch (IOException e) {
            System.out.println("proxySocket.close:"+e);
        }
    }
}

/**
 * ProxyThread is an HTTP proxy for the HTTP client.
 *
 * @xxxxxxxxxx java.net.Socket
 * @xxxxxxxxxx java.lang.Thread
 */
class ProxyThread extends Thread implements IModel {
    private Controller cont = new Controller();
    public void addIView(IView view) { cont.addIView(view); }
    public void deleteIView(IView view) { cont.deleteIView(view); }
    public void deleteIViews() { cont.deleteIViews(); }
    
    Socket from;
    Socket to;
    public ProxyThread(Socket fromSocket, Socket toSocket) {
        this.from = fromSocket;
        this.to = toSocket;
    }
    public void run() {
        try {
            DataInputStream in = new DataInputStream(from.getInputStream());
            DataOutputStream out = new DataOutputStream(to.getOutputStream());
            byte bbuf[] = new byte[1024];
            int rlen;
            while ((rlen = in.read(bbuf)) != -1) {
                cont.notifyIViews(this, new Int(rlen));
                out.write(bbuf, 0, rlen);
            }
            in.close();
            out.close();
        } catch (Exception e) {
            System.out.println("run:"+e);
        }
    }
}

class Log implements IView{
  FileWriter out = null;
  public Log(FileWriter out) { this.out = out; }
  public void update(Object o, Object change) {
    int c = ((Int)change).i;
    try { out.write(c); } catch (IOException e) { e.printStackTrace(); }
  }
}
====================================================================

IModel.java
====================================================================
// MVC utility
// jp.co.timedia.util.IModel.java
// copyright(c) 1998 yasuyuki@xxxxxxxxxx

package jp.co.timedia.util;

public interface IModel {
  public void addIView(IView view);
  public void deleteIView(IView view);
  public void deleteIViews();
}

====================================================================

IView.java
====================================================================
// MVC utility
// jp.co.timedia.util.IView.java
// copyright(c) 1998 yasuyuki@xxxxxxxxxx

package jp.co.timedia.util;

public interface IView {
  public void update(Object o, Object change);
}
====================================================================

Controllor.java
====================================================================
// MVC utility
// jp.co.timedia.util.Controller.java
// copyright(c) 1998-1999 yasuyuki@xxxxxxxxxx

package jp.co.timedia.util;

import java.util.*;

public class Controller {
  private Vector views = new Vector();
  
  public synchronized void addIView(IView view) {
    this.views.addElement(view);
  }

  public synchronized void deleteIView(IView view) {
    this.views.removeElement(view);
  }

  public synchronized void deleteIViews() {
    this.views.removeAllElements();
  }
  
  public synchronized void notifyIViews(Object o, Object change) {
    Enumeration e = this.views.elements();
    while (e.hasMoreElements()) {
      IView v = (IView)e.nextElement();
      v.update(o, change);
    }
  }
}

====================================================================
-- 
えんどう やすゆき <yasuyuki@xxxxxxxxxx>
http://www.javaopen.org/jfriends/ (Java互助会ホームページ)