10 de nov. de 2015

Using Jsoup to Http connection features in Java

Jsup is a friendly api to help us on Java EE development.

With it we can stop using HttpUrlConnection and their limitations , making your java code smaller and simpler.

we will now set it on a project using maven in 5 steps, Let's GO:

1) With new Java EE project using maven, add Jsoup dependency:

<dependencies>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.7.2</version>
        </dependency>

    </dependencies>


2) Import your classes:

import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;


3) Creation of new Response:

Response response = Jsoup.connect(url).timeout(0).ignoreHttpErrors(true).execute();                

4) Check status code:
System.out.println(response.statusCode());
 

5) Simple check redirect (301) url example code, using Jsoup:
   
 if ("http://expected.destiny.com".equals(
response.url().toString() )  ) {
            // Yes, my RewriteRule is OK

 } else {
            // No, my RewriteRule have errors
 }

* Tip: This code can help you to check Apache RewriteRule configs and automate our tests (you can use Junit and add this code into Tests code too).

Nenhum comentário:

Postar um comentário