package com.bullshit.webcrawler.client.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List; import com.bullshit.storage.entity.AuthUser;
import com.bullshit.storage.entity.Proxy;
import com.bullshit.storage.entity.vo.AuthUsersVo;
import com.fasterxml.jackson.databind.ObjectMapper; public class ProxyCollectionRestService { private static final String targetURL = "http://localhost:8080/blueberry/rest/v1/api/proxies"; public Proxy[] getAllProxies(){ Proxy[] proxies = null;
try {
URL restServiceURL = new URL(targetURL);
HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Accept", "application/json");
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : "
+ httpConnection.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream()))); String output;
ObjectMapper objectMapper = new ObjectMapper();
while ((output = responseBuffer.readLine()) != null) {
proxies = objectMapper.readValue(output, Proxy[].class);
List<Proxy> myObjects22 = objectMapper.readValue(output, objectMapper.getTypeFactory().constructCollectionType(List.class, Proxy.class));
System.out.println(myObjects22.size());
break;
}
httpConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return proxies;
} public AuthUsersVo postAuthUserInfo(AuthUser record){ AuthUsersVo authUsersVo = null;
try { URL targetUrl = new URL(targetURL); HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json"); ObjectMapper objectMapper = new ObjectMapper();
String input = objectMapper.writeValueAsString(record); OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(input.getBytes());
outputStream.flush(); if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ httpConnection.getResponseCode());
} BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream()))); String output;
System.out.println("Output from Server:\n");
while ((output = responseBuffer.readLine()) != null) {
System.out.println(output);
} httpConnection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
return authUsersVo;
} public static void main(String[] args) {
ProxyCollectionRestService service = new ProxyCollectionRestService(); Proxy[] s = service.getAllProxies(); System.out.println(s.length);
} }

  

Java 调用restful webservice & jackson的更多相关文章

  1. java调用restful webservice(转)

    一般来说,大家只会用到GET和POST方法来调用. GET方法的话,参数可以写在url里面. 比如说server的interface用的是@RequestParam或者@PathVariable,在客 ...

  2. Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法 转

    Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法   同事遇到一个很囧的问题,java调,netwebservice的时候,调用无参数方法成功,调用有参 ...

  3. Java 调用Restful API接口的几种方式--HTTPS

    摘要:最近有一个需求,为客户提供一些Restful API 接口,QA使用postman进行测试,但是postman的测试接口与java调用的相似但并不相同,于是想自己写一个程序去测试Restful ...

  4. java调用C# webService发布的接口

    java调用C# webService发布的接口 java调用C# webService方式有很多种我这里只介绍一种 首先需要引入axis的jar包 axis的maven坐标如下 <depend ...

  5. Java调用RestFul接口

    使用Java调用RestFul接口,以POST请求为例,以下提供几种方法: 一.通过HttpURLConnection调用 1 public String postRequest(String url ...

  6. 三种方法实现java调用Restful接口

    1,基本介绍 Restful接口的调用,前端一般使用ajax调用,后端可以使用的方法比较多, 本次介绍三种: 1.HttpURLConnection实现 2.HttpClient实现 3.Spring ...

  7. JAVA调用.NET WebService终极方案(包含对SoapHeader的处理)

    一.前言:      今日部门的产品需要用到短信功能,需要走公司统一的接口,而该短信接口是由.net开发的,利用两天时间彻底搞定了用java来调用.net 的web service,包括对soap h ...

  8. Java调用.NET webservice方法的几种方式

    最近做项目,涉及到web-service调用,现学了一个星期,现简单的做一个小结.下面实现的是对传喜物流系统(http://vip.cxcod.com/PodApi/GetPodStr.asmx?ws ...

  9. java调用 C# webservice接口

    java调用webservice接口方式有多种,本人很懒,测试一种满足我的需求,故为试验其他方法,仅供参考 一:工具 MyEclipse,C#编码发布的webservice接口 二:步骤 1.打开my ...

随机推荐

  1. mysql 登录的时候设置编码 utf8

    很多时候 导入sql 的时候需要命令行导入 但是有的时候容易出现乱码 因为字符集的问题 所以 尽量在登录的时候就设置编码 就不容易出错了 例如: mysql -uroot -p –default-ch ...

  2. c++新特性实验(2)类型特性

    1. 基本类型 1.1 增加 long long long long int signed long long signed long long int unsigned long long unsi ...

  3. 【xlwings1】快速入门

    前言:安装 pip install xlwings xlwings 安装成功后,如果运行提示报错“ImportError: no module named win32api”,请再安装 pypiwin ...

  4. Linux 拷贝有更改的文件

    cp -Ruv /home/username/trunk_new/app/* /data/httpdocs/wwwroot/app/

  5. WPF学习笔记-用Expression Blend制作自定义按钮

    1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点击此按钮,选择Edit control parts(template)>Edit a cop ...

  6. 手残,盘符前边多打一个空格导致的message d:\WEB_APP_QuChongFu\file\五月.xlsx (文件名、目录名或卷标语法不正确。)

    尝试读取并解析一个excel文件,一直提示错误 但是有个原始数据,导入就没问题 对比了一下,好像也就是字母d的大小写有区别 我先把大写的D改成小写的试试,如果是大小写问题,那应该抛出异常 好吧,好像并 ...

  7. day 56

    目录 聚合查询 分组查询 F与Q查询 ORM字段及参数 13个字段操作总结 自定义char字段 ORM中事物的操作 数据库三大范式 聚合查询 aggregate()是QuerySet()的一个终止子句 ...

  8. 【洛谷】 P1420 最长连号

    题目描述 输入n个正整数,(1<=n<=10000),要求输出最长的连号的长度.(连号指从小到大连续自然数) 输入输出格式 输入格式: 第一行,一个数n; 第二行,n个正整数,之间用空格隔 ...

  9. XtraBackup构建MySQL主从环境的方法

    环境:HE3主库,HE1从库HE1:192.168.1.248HE3:192.168.1.250从库my.cnf加入以下参数并重启数据库:read_only=1log_slave_updates=1( ...

  10. C# dataGridView_CellValueChanged事件

    C# 输入完以后立即更新缓冲区(DataGridView CheckBox列checked变化后就触发CellValueChanged事件) 在DataGridView添加如下的事件( Current ...