contoh program aplikasi client yang akan menggunakan perintah HTTP untuk mengambil dokumen yang ada melalui protokol HTTP. seperti contoh program dibawah ini :
import
java.io.*;
import
java.net.*;
public
class ExHTTPClient {
public
static void main(String args[])
{
try
{
Socket
clientSocket = new Socket(args[0],80);
System.out.println("Client:"+clientSocket);
getHTML(clientSocket,args[1]);
}
catch
(UnknownHostException e)
{System.out.println(e);}
catch
(IOException e)
{System.err.println(e);}
}
public
static void getHTML(Socket clientSocket,String fileName)
{
try
{
DataOutputStream
outbound = new DataOutputStream(
clientSocket.getOutputStream());
DataInputStream
inbound = new DataInputStream(
clientSocket.getInputStream());
outbound.writeBytes("GET"
+ fileName + "HTTP/1.0\r\n\r\n");
String
responseLine;
while
((responseLine = inbound.readLine())
!= null)
{
System.out.println(responseLine);
}
outbound.close();
inbound.close();
clientSocket.close();
}
catch
(IOException e)
{System.out.println(e);}
}
}
Tidak ada komentar:
Posting Komentar