java - 在java中使用socket填充html表单

标签 java sockets networking

嘿,我正在尝试使用 java 程序填写 html 表单,但我卡在了一半。实际上我能够获取页面,但无法将其写回服务器,或者可能能够写回但服务器没有响应。

这是我的程序:

import java.net.*;
import java.io.*;

public class fillForm{
    public static void main(String args[]){
        Socket s = null;
        try{
            s = new Socket("localhost", 80);
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            /******************
              Now download the page from the server.
             ******************/
            bw.write("GET /phpsandbox/form.html HTTP/1.1\n");
            bw.write("Host: localhost:80\n\n");
            bw.flush();
            readResponse(br);
            //now i have read whole input now its time to write output.
            bw.write("GET /phpsandbox/form.php?uName=hello HTTP/1.1\n");
            bw.write("Host: localhost:80\n\n");
            bw.flush();
            readResponse(br);
        }catch(IOException e){
            System.out.println("IO: " + e.getMessage());
        }catch(Exception e){
            System.out.println("Exception: " + e.getMessage());
        }                   
    }
    public static void readResponse(BufferedReader br){
        String newLine;
        try{
            while((newLine = br.readLine())!=null){
                System.out.println("Line: " + newLine);
            }
        }catch(IOException e){
            System.out.println("IO: " + e.getMessage());
        }
    }
}

这里是 form.html

<html>
<head><title>form</title></head>
<body>
<form action="form.php" method="GET">
<label>Enter name</label>
<input name="uName"/>
<input type="submit" />
</form>
</body>
</html>

这里是 form.php 与 form.html 位于同一文件夹中

<?php
        //read the response from the client
        echo "hELLO";
        echo $_GET['uName'];
?>

这是输出:

Line: HTTP/1.1 200 OK
Line: Date: Sun, 06 Feb 2011 13:46:17 GMT
Line: Server: Apache/2.2.11 (Unix) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8k PHP/5.2.9 mod_apreq2-20051231/2.6.0 mod_perl/2.0.4 Perl/v5.10.0
Line: Last-Modified: Sun, 06 Feb 2011 13:29:58 GMT
Line: ETag: "6c3c-b5-49b9d1c8f56c1"
Line: Accept-Ranges: bytes
Line: Content-Length: 181
Line: Content-Type: text/html
Line: 
Line: <html>
Line: <head><title>form</title></head>
Line: <body>
Line: <form action="form.php" method="GET">
Line: <label>Enter name</label>
Line: <input name="uName"/>
Line: <input type="submit" />
Line: </form>
Line: </body>
Line: </html>

输出程序等待一段时间后退出。

谢谢:)

最佳答案

阅读 HTTP 协议(protocol);例如,看看这里:http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html 。我认为它需要在第一个 GET 和第二个 GET 之间有几个空行。

如果失败,请关闭套接字并打开一个新套接字,这样应该可以工作。

关于java - 在java中使用socket填充html表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4913720/

相关文章:

java - 在 IBM Rational Software Architect for Java6 Maven 安装中收到致命警报 : protocol_version to https://repo. maven.apache.org/maven2/

客户端端口号在服务器 TCP/IP 套接字中显示错误 C 语言编程

php - 如何检测流套接字中的中止连接-PHP

linux - nftables中表/链的顺序是怎么排列的?

linux - 需要将输出附加到文件

java, swing - 在 JScrollPane 中添加 JlayeredPane 仅显示空白背景

java - StaticApplicationContext 中的 Autowiring bean

java - java8中如何将对象数组转换为用户定义的对象

c - 在一个只读 fd 上进行 IO 多路复用是否比简单地阻塞读取有助于获得更好的性能?

ubuntu - 通过网桥为容器分配静态 IP