import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class HttpConnection
{
// Holds target port number for Connection.
private int HTTP_PORT = 80;
// Holds target url.
private String resource;
// Holds target server address.
private String host;
// Holds target file address.
private String file;
// Setting defalt HTTP request method to Get.
private String method = "GET";
// Holds body length of request.
private String contentLen = "";
// Setting default content Type to text/html.
private String type = "text/html";
private String contentType;
// Socket object for connection.
private Socket httpSocket=null;
// Holds all HTTP headers that our class will initialize.
private StringBuffer defaultHTTPHeaders;
// Holds all HTTP headers that user will initialize.
private StringBuffer
userDefinedHTTPHeaders = new StringBuffer();
private InputStream in=null;
private OutputStream out=null;
// Constructor with URL and Port number as argument.
// It opens a socket connection at provided URL.
public HttpConnection (String url, int port)
throws UnknownHostException, IOException
{
if (port > 0 )
HTTP_PORT = port;
// Extracting file and host addresses from the URL.
resolveURL(url);
httpSocket = new Socket(host,HTTP_PORT);
defaultHTTPHeaders = new StringBuffer();
}//HttpConnection()
// Returns input stream for received data.
public InputStream getInputStream() throws IOException
{
return in = httpSocket.getInputStream();
}//getInputStream()
// Returns output stream for data to be sent.
public OutputStream getOutputStream() throws IOException
{
return out = httpSocket.getOutputStream();
}//getOutputStream()
// Parses the passed URL e.g.
"http://www.xyz.com/tariqspage/soap "
// and extract Host address i.e. "www.xyz.com"
// and File name i.e. "/tariqspage/soap" from URL.
private void resolveURL(String url)
{
resource = url.substring (7);
int slashpos = resource.indexOf ("/");
file = resource.substring (slashpos);
host = resource.substring (0,slashpos);
}//resolveURL()
// Set request method for http request.
public void setRequestMethod (String method)
{
this.method = method;
}
// Set Content Type Header for request.
public void setContentType (String type)
{
this.type = type;
}//setContentType
// Sets content length of the request data.
public void setContentLength (int length)
{
contentLen =
"Content-length:"+String.valueOf(length)+"\r\n";
}//setDataLength()
// Returs Content Type Header.
public String getContentType ()
{
System.out.println("GetContentType Called.......");
return this.contentType;
}//getContentType ()
// Returs Request Method Header.
public String getRequestMethod ()
{
return this.method;
}//getRequestMethod()
// Sets additional user defined headers.
public void setRequestProperty (String name, String value)
{
userDefinedHTTPHeaders.append(name+" "+value+"\r\n");
}//setRequestProperty()
// Concatenating headers from buffer strings
// into a single String and Returns it.
public String getHeaders()
{
defaultHTTPHeaders.append(method+"
"+file+" HTTP/1.1\r\n");
defaultHTTPHeaders.append("Host: "+host+"\r\n");
defaultHTTPHeaders.append
("Content-type: "+type+"; charset=utf-8\r\n");
defaultHTTPHeaders.append(contentLen);
// Appending empty line at end of headers.
userDefinedHTTPHeaders.append("\r\n");
// Concatenating two string buffers containing headers
// into single String named "Headers".
String headers = defaultHTTPHeaders.toString();
headers +=userDefinedHTTPHeaders.toString();
return headers;
}//getHeaders()
}//class
|