zoukankan      html  css  js  c++  java
  • webMagic 处理get请求

    例如URL:
    
    http://localhost:8080/javascritp/queryList/?params={"name":"aaa","limit":"45","sl":"show"} 
    
    应该是分成两个url来处理如:
    
    String url1 = "http://localhost:8080/javascritp/queryList/?params=";
    
    String url2 = URLEncoder.encode("{"name":"aaa","limit":"45","sl":"show"}","UTF-8");
    
    //请求URL
    
    
    public static String get(String url, Map<String, String> headers, String charset, Integer connTimeout,
    Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception {
    HttpClient client = null;
    HttpGet get = new HttpGet(url);
    String result = "";
    try {
    if (headers != null && !headers.isEmpty()) {
    for (Entry<String, String> entry : headers.entrySet()) {
    get.addHeader(entry.getKey(), entry.getValue());
    }
    }
    // 设置参数
    Builder customReqConf = RequestConfig.custom();
    if (connTimeout != null) {
    customReqConf.setConnectTimeout(connTimeout);
    }
    if (readTimeout != null) {
    customReqConf.setSocketTimeout(readTimeout);
    }
    get.setConfig(customReqConf.build());
    
    HttpResponse res = null;
    
    if (url.startsWith("https")) {
    // 执行 Https 请求.
    client = createSSLInsecureClient();
    res = client.execute(get);
    } else {
    // 执行 Http 请求.
    client = HttpClientUtils.client;
    res = client.execute(get);
    }
    
    result = IOUtils.toString(res.getEntity().getContent(), charset);
    } finally {
    get.releaseConnection();
    if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) {
    ((CloseableHttpClient) client).close();
    }
    }
    return result;
    }
    
    //WebMagic处理get请求
    
    Request request = new Request();
    request.setMethod(HttpConstant.Method.GET);
    request.setUrl(url1+url2);
    
    //启动爬虫
    Spider.create(new PageProcessor())
    .addPipeline(new Pipline())
    .addRequest(request)
    .run();
    
     
    
  • 相关阅读:
    Extjs打开window窗口自动加载html网页
    CSS预处理器之SASS用法指南
    HmacSHA256摘要算法
    Base64编解码
    孔子困于陈蔡故事(转载)
    我的2019
    给Oracle字段和表加注释
    【JDBC】使用properties连Oracle数据库,使用DatabaseMetaData获取字段的注释
    [JDBC]查询结果集把字段名和字段值一起竖向输出
    [Java/Reflect]使用反射机制获得一个对象的属性名和属性值
  • 原文地址:https://www.cnblogs.com/joinlemon/p/9541863.html
Copyright © 2011-2022 走看看