HTTP Protocol - Create a Simple HTTP Client
Http Client: To send a http request to a web server we need a http client to communicate with the server and send request message to it. We generally do this by using web browsers.
Customers request a server via web browsers and then that server may need data access or some other kind of helps from another server/servers. So, here the question arises that is "How server can request to other server?" as it doesn't have any http client in it as like the browsers.
Here we need to use Apache HTTP client, Ok HTTP client and so many others provided by several vendors to make a http request.
Here is a simple example that creates a custom http client to browse a given url using Apache
HTTP client:
Source:
public class MyHttpClient {
//"http://www.tariqnotes.blogspot.com/"
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter an url to browse:");
String url = sc.nextLine();
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
}
Create HTTP Client Project: Follow the steps mentioned below -
Step #1: Create a Maven Project
Open eclipse -> File -> New -> Project -> Maven -> Project
Step #2: Add the following dependency to your maven pom.xml file:
<dependencies>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
Step #3: Create a new Java class MyHttpClient.java and add the codes above.
Step #4: Create Executable Jar including Maven dependencies. To do so you need to add the following build plugin to your maven pom.xml file:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.ssm.tariq.MyHttpClient</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Step #5: Now run a maven build for your Maven Project to create your executable jar.
Right click on your project -> select Run As -> Maven Build -> Set Goal ->
Create a Linux Command to Use your HTTP Client: To create your own linux command (I've used Ubuntu 16.04) and run your http client as like curl or any other client available in the market, follow the steps below:
We will create a new linux command jurl. Then we will put it on linux console along with a url and wanna get http response.
Step #1: Go to the source target folder and rename your jar to jurl.jar
Step #2: Create a new folder jurl in your Linux home directory and copy your jurl.jar to that folder.
Step #3: Create a new file jurl without any extension and add the following shell scripts:
PATH=$PATH:/home/user/jurl
Step #5: Save and run the command - jurl http://tariqnotes.blogspot.com
Now use your own http client.
Download Project: https://github.com/ssmtariq/SimpleHttpClient.git
Download Jar: http://www.mediafire.com/file/2blcxv61cccjb82/jurl.zip
Create HTTP Client Project: Follow the steps mentioned below -
Step #1: Create a Maven Project
Open eclipse -> File -> New -> Project -> Maven -> Project
Step #2: Add the following dependency to your maven pom.xml file:
<dependencies>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
Step #3: Create a new Java class MyHttpClient.java and add the codes above.
Step #4: Create Executable Jar including Maven dependencies. To do so you need to add the following build plugin to your maven pom.xml file:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.ssm.tariq.MyHttpClient</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Step #5: Now run a maven build for your Maven Project to create your executable jar.
Right click on your project -> select Run As -> Maven Build -> Set Goal ->
clean package assembly:single
Create a Linux Command to Use your HTTP Client: To create your own linux command (I've used Ubuntu 16.04) and run your http client as like curl or any other client available in the market, follow the steps below:
We will create a new linux command jurl. Then we will put it on linux console along with a url and wanna get http response.
Step #2: Create a new folder jurl in your Linux home directory and copy your jurl.jar to that folder.
Step #3: Create a new file jurl without any extension and add the following shell scripts:
#!/bin/sh
echo $1
java -jar /home/user/jurl/jurl.jar $1
Step #4: Now open your .bashrc file at linux home and add the path:
#JURLPATH=$PATH:/home/user/jurl
Step #5: Save and run the command - jurl http://tariqnotes.blogspot.com
Now use your own http client.
Download Project: https://github.com/ssmtariq/SimpleHttpClient.git
Download Jar: http://www.mediafire.com/file/2blcxv61cccjb82/jurl.zip
I am really happy to read your blog. If any one wants to buy cheap rate logo, please click on Professional Logo Design
ReplyDelete