在java中使用套接字填充html表单
嘿,我正试图用java程序填充html表单,但我坚持了一半。实际上,我能够获取页面,但无法将其写回服务器,或者可能能够将其写回,但服务器没有响应 这是我的节目:在java中使用套接字填充html表单,java,sockets,networking,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
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>
<?php
//read the response from the client
echo "hELLO";
echo $_GET['uName'];
?>
形式
输入名称
这里是与form.html位于同一文件夹中的form.php
<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>
<?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/1.1200正常
线路:日期:2011年2月6日星期日13:46:17 GMT
行:服务器: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
行:最后修改:Sun,2011年2月6日13:29:58 GMT
行:ETag:“6c3c-b5-49b9d1c8f56c1”
行:接受范围:字节
行:内容长度:181
行:内容类型:text/html
行:
行:
行:表单
行:
行:
行:输入名称
行:
行:
行:
行:
行:
给出输出后,程序等待一段时间,然后退出
谢谢:)阅读HTTP协议;请在此处查看实例:。我想在第一个GET和第二个GET之间需要几行空白
如果失败,请关闭套接字并打开一个新的套接字,这样应该可以工作。阅读HTTP协议;请在此处查看实例:。我想在第一个GET和第二个GET之间需要几行空白
如果失败,请关闭插座并打开一个新的插座,这样应该可以工作。为什么要这样做?为什么不编写简单的HTTPServlet呢?您是否考虑过使用更合适的库,例如ApacheHttpComponents@亚历克赛:如何使用HTTP Servlet?@Tom:我会试试这个,但你能告诉我如何使用套接字吗?你为什么要这样做?为什么不编写简单的HTTPServlet呢?您是否考虑过使用更合适的库,例如ApacheHttpComponents@AlexR:如何使用HTTP Servlet?@Tom:我会试试这个,但你能告诉我如何使用套接字吗??