a very basic .java HTTP client program Pls post .java or program that compiles b
ID: 3557365 • Letter: A
Question
a very basic .java HTTP client program
Pls post .java or program that compiles
basic HTTP client program. As discussed in class, web pages are transmitted using HTTP,
a standard for transmitting data across the internet. HTTP requests are sent out by your web
browser, and a web server sends back the requested resource as an HTTP response. The
format of these are specified in RFC2616. We will be looking specifically at the format for
requests and responses. HTTPClient.java and HTTPServer.java
Instructions:!
We will create a simple HTTP client that sends requests to a host (another word for the
computer hosting the site you are interested in). Your program will take a hostname as a
program argument, send an HTTP GET request to ask that host for a page, and print back the
response from the server. !
1. Create a new .java file called HTTPClient.java containing a public class HTTPClient
with a main method. Put all of your code in this main method. !
2. Your client will take in one argument- the hostname to connect to. This is a string that
identifies what computer your request is intended for (google.com, cs.sfsu.edu, and
www.amazon.com are all examples of hostnames).
Check that the args array passed into the main method has a length of 1. If an
improper number of arguments was provided print a message with usage instructions:
System.out.println("Usage: java HTTPClient <host name>
Explanation / Answer
import java.io.*;
import java.net.*;
public class Myclient1
{
Socket s;
DataInputStream din;
DataOutputStream dout;
public Myclient1()
{
try{
s=new Socket("localhost",10);
din=new DataInputStream(s.getInputStream());
dout=new DataOutputStream(s.getOutputStream());
clientChat();
}catch(Exception e){System.out.println(e);}
}
public void clientChat()throws IOException
{
My m=new My(din);
Thread t1=new Thread(m);
t1.start();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1;
do{
s1=br.readLine();
dout.writeUTF(s1);
dout.flush();
}while(!s.equals("stop"));
}
public static void main(String s2[])
{
new Myclient1();
}
}
class My implements Runnable
{
DataInputStream din;
My(DataInputStream din)
{
this.din=din;
}
public void run()
{
String s2=" ";
do{
try{
s2=din.readUTF();
System.out.println(s2);
}catch(Exception e){}
}while(!s2.equals("stop"));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.