项目作者: sadeghpro

项目描述 :
JWeb is a Java library for connecting to Http servers and fetch html or json data.
高级语言: Java
项目地址: git://github.com/sadeghpro/JWeb.git
创建时间: 2019-04-11T18:21:34Z
项目社区:https://github.com/sadeghpro/JWeb

开源协议:MIT License

下载


JWeb: Java Http Connection

JWeb is a Java library for connecting to Http servers and fetch html or json data.

Getting start

Maven

Add JitPack to maven repositories:

  1. <repositories>
  2. <repository>
  3. <id>jitpack.io</id>
  4. <url>https://www.jitpack.io</url>
  5. </repository>
  6. </repositories>

Add the dependency:

  1. <dependency>
  2. <groupId>com.github.sadeghpro</groupId>
  3. <artifactId>jweb</artifactId>
  4. <version>0.9.0</version>
  5. </dependency>

Gradle

Add this in your root build.gradle at the end of repositories:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://www.jitpack.io' }
  5. }
  6. }

and then in dependencies:

  1. dependencies {
  2. implementation 'com.github.sadeghpro:jweb:0.9.0'
  3. }

Example

Fetch the Wikipedia homepage:

  1. JWeb web = new JWeb();
  2. Response response = web.connect("https://en.wikipedia.org/wiki/Main_Page")
  3. .setMethod(Method.GET)// optional default is GET
  4. .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")
  5. //.setBody("username=name&password=pass") // set body
  6. .setTimeout(1000)
  7. .exec();
  8. System.out.println(response.getStatusCode() + ":" + response.getBody());

Set default header and default url:

  1. JWeb web = new JWeb();
  2. Map<String, String> headers = new HashMap<>();
  3. headers.put("Content-Type", "application/json");
  4. web.setDefaultHeaders(headers);
  5. web.setDefaultUrl("https://en.wikipedia.org/wiki"); // only work when use JWeb.connect with string parameter
  6. web.connect("/Main_Page").exec();

You can deserialize response body:

  1. JWeb web = new JWeb();
  2. Response response = web.connect("/products").exec();
  3. List<Product> products = r.deserializeJsonArray(Product.class);
  4. response = web.connect("/products/1").exec();
  5. Product product = r.deserializeJsonObject(Product.class);

Open source

JWeb is an open source project distributed under the liberal MIT license. The source code is available at GitHub.