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

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




>> 成功している方のGET内容をダンプして検討してみるとよいかも知れません。
>
>  ありがとうございます。
>  これは ProxyServerとして動作するクラスですね?
>  コンパイルを試してみましたが、Intクラスが無いようで、コンパイルが
>通らないです。
>
>#でも、何でGETコマンドが失敗するのだろう。
>#しかも、HEADコマンドだと Bad Request と言われるし...

Java からGETするときは、こんなGETメソッドを発行しているようです。

GET http://www.j-teens.com/pd1J/f1/main.html HTTP/1.0
User-Agent: Java1.2.1
Host: www.j-teens.com
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

あと、私のサンプルコードは大嘘でした。

コンソールに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(inlog));
            requester.addIView(new Log());
            requester.start();
            ProxyThread responder = new ProxyThread(serverSocket, clientSocket);
            //responder.addIView(new Log(outlog));
            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 String(bbuf));
                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) {
    System.out.print((String)change);
    //try { out.write((String)change); } catch (IOException e) { e.printStackTrace(); }
  }
}
-- 
えんどう やすゆき <yasuyuki@xxxxxxxxxx>
http://www.javaopen.org/jfriends/ (Java互助会ホームページ)