hupubao-common-http

http请求访问工具

License

License

GroupId

GroupId

com.hupubao
ArtifactId

ArtifactId

hupubao-common-http
Last Version

Last Version

0.0.6
Release Date

Release Date

Type

Type

jar
Description

Description

hupubao-common-http
http请求访问工具
Project URL

Project URL

https://github.com/ysdxz207/hupubao-common-http
Source Code Management

Source Code Management

https://github.com/ysdxz207/hupubao-common-http

Download hupubao-common-http

How to add to project

<!-- https://jarcasting.com/artifacts/com.hupubao/hupubao-common-http/ -->
<dependency>
    <groupId>com.hupubao</groupId>
    <artifactId>hupubao-common-http</artifactId>
    <version>0.0.6</version>
</dependency>
// https://jarcasting.com/artifacts/com.hupubao/hupubao-common-http/
implementation 'com.hupubao:hupubao-common-http:0.0.6'
// https://jarcasting.com/artifacts/com.hupubao/hupubao-common-http/
implementation ("com.hupubao:hupubao-common-http:0.0.6")
'com.hupubao:hupubao-common-http:jar:0.0.6'
<dependency org="com.hupubao" name="hupubao-common-http" rev="0.0.6">
  <artifact name="hupubao-common-http" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.hupubao', module='hupubao-common-http', version='0.0.6')
)
libraryDependencies += "com.hupubao" % "hupubao-common-http" % "0.0.6"
[com.hupubao/hupubao-common-http "0.0.6"]

Dependencies

compile (6)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.26
com.alibaba : fastjson jar 1.2.58
org.jsoup : jsoup jar 1.11.2
org.apache.httpcomponents : httpclient jar 4.5.8
org.apache.commons : commons-lang3 jar 3.9
com.hupubao : hupubao-common-utils jar 0.0.6

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

简单封装http请求工具

<dependency>
    <groupId>com.hupubao</groupId>
    <artifactId>hupubao-common-http</artifactId>
    <version>${hupubao-common-http.version}</version>
</dependency>

版本自行查看:https://mvnrepository.com/artifact/com.hupubao/hupubao-common-http

Http工具

  • 起因:爬小说数据时发现有些页面jsoup不能爬到小说内容, 而HttpClient可以,又不想用太大的爬虫框架, 于是自己动手把HttpClient封装了一下

  • 使用方法

import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import win.hupubao.common.http.Page;

public class TestPage {
    public static void main(String[] args) {
        JSONObject paramsJson = new JSONObject();//json参数,当然也可以用Map
        paramsJson.put("q", "太阳");

        String paramsXml = "<xml><id>123456</id><name>大帅比</name></xml>";//raw参数,post方式
        Page.Response responseGet = Page.create()
                .connectionTimeout(3000)
                .retryTimes(3)
                .request("https://www.google.com/search",
                        paramsJson,
                        Connection.Method.GET);

        System.out.println(responseGet);

        Page.Response responsePost = Page.create()
                .connectionTimeout(3000)
                .retryTimes(3)
                .request("https://www.google.com/search",
                        paramsXml,
                        Connection.Method.POST);

        System.out.println(responsePost.toJson(false));
    }
}
  • 重要

本工具因为使用了Jsoup解析xml,可以防止xxe攻击问题。 原xml:

<?xml version="1.0" encoding="utf-8"?>
        <!DOCTYPE xdsec [
                <!ELEMENT methodname ANY >
                <!ENTITY xxe SYSTEM "file:///C:/Users/Administrator/Desktop/aa.txt" >]>
<test>
    <content>&xxe;</content>
</test>

Jsoup会把xml解析成如下:

<!--?xml version="1.0" encoding="utf-8"?--><!doctype xdsec>
<!--ENTITY xxe SYSTEM "file:///C:/Users/Administrator/Desktop/aa.txt" -->
<html>
 <head></head>
 <body>
  ]&gt; 
  <test> 
   <content>
    &amp;xxe;
   </content> 
  </test>
 </body>
</html>

Versions

Version
0.0.6
0.0.1