在java中使用socket填充html表单

发布于 2024-10-16 15:42:26 字数 2840 浏览 3 评论 0原文

嘿,我正在尝试使用 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>

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

谢谢:)

Hey all I am trying to fill a html form using a java program but i stuck half way. actually i am able to fetch the page but can't write it back to server or possibly able to write it back but with no response from server.

Here is my Program:

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());
        }
    }
}

nd here is 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>

and here is form.php residing in same folder as the form.html

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

And here is the output:

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>

After giving the output program waits for sometime then exits.

Thanks:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉月流沐 2024-10-23 15:42:26

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

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

Read up on HTTP protocol; take a look here for instance: http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html. I think it wants a couple of blank lines between the first GET and the second one.

If that fails, close the socket and open a new one, that should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文