HttpClient

By: aathishankaran Emailed: 1700 times Printed: 2205 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

HttpClient

             The HttpClient class does the real network communication. It simply sends the designated request line and request headers to the Web server, then reads the lines that come back one at a time, placing them into a TextArea until either the server closes the connection or the HttpClient is interrupted by means of the isInterrupted flag.

String portString = portField.getTextField().getText();

if (portString.length() == 0) {

report("Missing port number");

return(false);

}

try {

port = Integer.parseInt(portString);

} catch(NumberFormatException nfe) {

report("Illegal port number: " + portString);

return(false);

}

requestLine =

requestLineField.getTextField().getText();

if (requestLine.length() == 0) {

report("Missing request line");

return(false);

}

getRequestHeaders();

return(true);

}

private void report(String s) {

resultArea.setText(s);

}

private void getRequestHeaders() {

for(int i=0; i<requestHeaders.length; i++)

requestHeaders[i] = null;

int headerNum = 0;

String header =

requestHeadersArea.getText();

StringTokenizer tok =

new StringTokenizer(header, "\r\n");

while (tok.hasMoreTokens())

requestHeaders[headerNum++] = tok.nextToken();

}

}

 

HttpClient.java

import java.awt.*;

import java.net.*;

import java.io.*;

 

public class HttpClient extends NetworkClient {

private String requestLine;

private String[] requestHeaders;

private TextArea outputArea;

private Interruptible app;

public HttpClient(String host, int port,

String requestLine, String[] requestHeaders,

TextArea outputArea, Interruptible app) {

super(host, port);

this.requestLine = requestLine;

this.requestHeaders = requestHeaders;

this.outputArea = outputArea;

this.app = app;

if (checkHost(host))

connect();

}

protected void handleConnection(Socket uriSocket)

throws IOException {

try {

PrintWriter out = SocketUtil.getWriter(uriSocket);

BufferedReader in = SocketUtil.getReader(uriSocket);

outputArea.setText("");

out.println(requestLine);

for(int i=0; i<requestHeaders.length; i++) {

if (requestHeaders[i] == null)

break;

else

out.println(requestHeaders[i]);

}

out.println();

String line;

while ((line = in.readLine()) != null &&

!app.isInterrupted())

outputArea.append(line + "\n");

if (app.isInterrupted())

outputArea.append("---- Download Interrupted ----");

} catch(Exception e) {

outputArea.setText("Error: " + e);

}

}


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
What are the different scopes in JSP?
JSP Program for display Date
JSP CheckBox Example
Embedding java codes in jsp sciptlets
Declaring variable in JSP
HttpClient
The Advantages of JSP
Referencing Windows
Deploying an Individual JSP on Tomcat
Getting HTTP Request Headers in a JSP
Getting Started with JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Most Emailed Articles (in JSP)
What is JSP?
JSP pages in servlet
Declaring variable in JSP
A Servlet That Generates HTML
Simple HTML-Building Utilities
Checkbox Object
Retrieving a Portion of a String
JSP Example to connect to MS SQL database using Tomcat Connection Pool
The Application Object in JSP
Cookies using JSP or Java Bean
Click to Activate and Use this control
The JSP Program running first Time.
Scriptlets
Server Side Programming
Syntax For JSP Declaratives