2011-02-06 2 views
1

안녕하세요, 저는 자바 프로그램을 사용하여 HTML 폼을 채우려하고 있습니다. 실제로 페이지를 가져올 수 있지만 다시 서버에 쓸 수 없거나 서버에서 응답이없는 상태로 다시 쓸 수는 없습니다. 여기자바에서 소켓을 사용하여 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.html

<?php 
     //read the response from the client 
     echo "hELLO"; 
     echo $_GET['uName']; 
?> 
으로 같은 폴더에 존재하는 form.php입니다 : 여기

내 프로그램입니다

그리고 출력은 다음과 같습니다.

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> 

출력 프로그램을 제공 한 후 가끔 기다린 다음 종료합니다.

고마워요 :)

+0

왜이 작업을 수행하고 있습니까? 왜 간단한 HTTP 서블릿을 작성하지 않습니까? – AlexR

+1

Apache HTTPComponents와 같은보다 적절한 라이브러리를 사용해 보셨습니까? http://hc.apache.org/ –

+0

@AlexR : HTTP 서블릿을 사용하는 방법? – codeomnitrix

답변

관련 문제