socket("123.123.123.123","80")
output stream write("GET /project/login.jsp HTTP/1.0\n\n");
read input stream

解决方案 »

  1.   

    好像需要给对方发送http的那些头信息才能收到相应!我不知道,瞎猜的!good luck
      

  2.   

    URL
    URLConnection
    这个可以。
      

  3.   

    取网页失败并不能说明创建socket失败你是怎么取网页内容的?需要向web server发送一个请求"GET / HTTP/1.1\r\n\r\n"
      

  4.   

    http://mail.phys-iasi.ro/Library/Computing/J_applets/ch9.htm
      

  5.   

    我的socket并没有创建失败~
    只是抛出异常没有找到响应的host
    因为123.123.123.123是不能访问的~
    只能访问123.123.123.123/project
    所以我向客户端的输出添加了输出流"GET /project HTTP/1.1\r\n\r\n"
    但是还是没有能找到·~我郁闷啊~~~
      

  6.   

    是没有访问权限吗??
    比如服务器端设置了某些根目录是不能被访问的参数等good luck
      

  7.   

    URL
    URLConnection
    试试 URL url = new URL("http://123.123.123.123/project/login.jsp");
      

  8.   

    加一个Host: 123.123.123.123
    因为现在的服务器都会检查host值的,多用于虚拟主机
    GET /project HTTP/1.1\nHost: 123.123.123.123\n\n
      

  9.   

    post有格式检查的.
    你post的格式对不对啊.
    我是这样post的:
    POST http://news.sina.com.cn/c/2004-12-21/10084584671s.shtml HTTP/1.0
    User-Agent: myselfHttp/1.0
    Accept: www/source; text/html; image/gif; */*
    Host: news.sina.com.cn
    Content-type: application/x-www-form-urlencoded
    Content-length: 30alumniuname=kkk&alumniupwd=kkksource:
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.URL;/*
     * Created on 2004-12-21
     * 
     * To change the template for this generated file go to Window - Preferences -
     * Java - Code Generation - Code and Comments
     *//**
     * @author Artimus
     * 
     * To change the template for this generated type comment go to Window -
     * Preferences - Java - Code Generation - Code and Comments
     */
    public class ConnectUrl {    private static final String OPEN_URL = "http://news.sina.com.cn/c/2004-12-21/10084584671s.shtml";    public static void main(String[] args) {
            Socket client = null;
            BufferedOutputStream sender = null;        try {
                URL webURL = new URL(OPEN_URL);
                InetAddress address = InetAddress.getByName(webURL.getHost());
                int portTemp = webURL.getPort();
                if (portTemp == -1) {
                    portTemp = 80;
                }
                client = new Socket(address, portTemp);
                String content = "alumniuname=kkk&";
                content += "alumniupwd=kkk";            String cmd = "POST " + OPEN_URL + " HTTP/1.0\r\n" + getBaseHeads();
                cmd += "Host: " + webURL.getHost() + "\r\n";
                cmd += "Content-type: application/x-www-form-urlencoded\r\n";
                cmd += "Content-length: " + content.length() + "\r\n\r\n";
                cmd += content + "\r\n";
                
    System.out.println(cmd);
    sender = new BufferedOutputStream(client.getOutputStream());
                sender.write(cmd.getBytes(), 0, cmd.length());
                sender.flush();            BufferedReader out = new BufferedReader(new InputStreamReader(
                        client.getInputStream()));
                int b;
                String line = null;
                int i = 0;
                if (out != null) {
                    while ((line = out.readLine()) != null) {
                        System.out.println(line);
                    }
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (client != null) {
                        client.close();
                        client = null;
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
        /** **************************** */
        public static String getBaseHeads() {
            String inf = "User-Agent: myselfHttp/1.0\r\n"
                    + "Accept: www/source; text/html; image/gif; */*\r\n";
            return inf;
        }}如果这样还不行,就不知道了