互联网API接口的简单demo实现(详细)

	废话不多说,我们直接进入正题!

环境:
开发工具:idea
java版本:jdk1.8
Maven

1.添加依赖:

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0</version>
        </dependency>
    </dependencies>

2.java代码:

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;

public class HttpTest {
     
    public static void main(String[] args) {
     
        //HttpClient可以理解为一个浏览器,但是又和浏览器不同
        HttpClient httpClient = new HttpClient();
        //详解可以去访问:https://blog.csdn.net/zhangzeyuaaa/article/details/49073727
        httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        try {
     
            getMessage(httpClient);
        } catch (Exception e) {
     
            e.printStackTrace();
        }
    }
    private static void getMessage(HttpClient httpClient) throws Exception {
     
        //要访问的接口
        String dataUrl="https://restapi.amap.com/v3/weather/weatherInfo?city=430602&key=你申请的key";
        //使用get请求获取数据
        GetMethod getMethod = new GetMethod(dataUrl);
        //执行
        httpClient.executeMethod(getMethod);
        //将响应体获取为字符串
        String responseBody = getMethod.getResponseBodyAsString();
        //解析
        JSONObject jsonObject = JSONObject.parseObject(responseBody);
        System.out.println(jsonObject.toJSONString());
    }
}

浏览器访问结果:
互联网API接口的简单demo实现(详细)_第1张图片
控制台打印结果:
互联网API接口的简单demo实现(详细)_第2张图片

说明:这个接口是高德的天气接口,需要去高德官网创建一个key
互联网API接口的简单demo实现(详细)_第3张图片
互联网API接口的简单demo实现(详细)_第4张图片
进入控制台创建一个新应用:
互联网API接口的简单demo实现(详细)_第5张图片
互联网API接口的简单demo实现(详细)_第6张图片
URL和请求参数说明:
互联网API接口的简单demo实现(详细)_第7张图片
搞完了,收工!

补充一下:
前些天正在看一本小说,唐家三少写的<酒神>,但是呢,看到后面各种错字少字,然后去网上找了一下无错字少字的,最后呢,找到一个接口,但是是html页面,我就随便写了个方法把html页面爬下来了,但是却发现无法获取到我想要的文字段数据,如果有朋友会的话可以联系我,下面我把代码贴上来:
pom.xml:

<dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.8</version>
        </dependency>
    </dependencies>

代码如下(有兴趣的朋友可以粘过去跑一下):

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.FileOutputStream;
import java.io.IOException;

@SuppressWarnings("all") //重复警报压制
public class XsDemo {
     
    public static void main(String[] args) throws Exception {
     //文章接口:http://m.mcmssc.com/71_71259/31076564.html
        long a = 31075758;
        int b = 2;
        for (int i = 0; i < 1; i++) {
     
            String c = "_" + b;
            String d = "http://m.mcmssc.com/71_71259/" + a;
            if("_2".equals(c)){
     
                //这里写第一节
                Thread.sleep(5000);
                //System.out.println(d+".html");
                httpDemo(d + ".html");
                //这里写第二节
                Thread.sleep(5000);
                //System.out.println(d + c+".html");
                httpDemo(d + c + ".html");
                b++;
            }
            if("_3".equals(c)){
     
                //进来这里写第三节
                Thread.sleep(5000);
                //System.out.println(d + c+".html");
                httpDemo(d + c + ".html");
                b--;
                //下一章
                a++;
            }
        }
        //若能提取出文本信息,请联系我QQ:1442404546,柳
    }
    public static void httpDemo(String d) throws IOException {
     
        //1.生成httpclient,相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求,相当于在浏览器地址栏输入 网址
        HttpGet request = new HttpGet(d);
        //3.执行get请求,相当于在输入地址栏后敲回车键
        response = httpClient.execute(request);
        //4.获取响应内容
        HttpEntity httpEntity = response.getEntity();
        String html = EntityUtils.toString(httpEntity, "utf-8");
        //打印内容
        //System.out.println(html);
        //写入txt
        if(html != null){
     
            FileOutputStream  writer;
            try {
     
                //true不覆盖已有内容
                writer = new FileOutputStream("C:/Users/asus/Desktop/lll.txt",true);
                //写入文件
                writer.write(html.getBytes());
                // 写入一个换行
                writer.write("\r\n".getBytes());
                writer.flush();
                writer.close();
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }
    }

}

写在最后:人若成就一道景致,则无关春夏秋冬!

你可能感兴趣的:(java,cookie)