添加依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.3</version>
</dependency>
工具类
public final static String get(String uri) throws IOException {
WebClient client = createClient(uri);
client.encoding("GBK");
Response response = client.get();
InputStream stream = (InputStream) response.getEntity();
return streamToString(stream);
}
protected static WebClient createClient(String uri) {
WebClient client = WebClient.create(uri);
String auth = "Basic "
+ Base64Utility.encode("kermit:kermit".getBytes());
client.header("Authorization", auth);
return client;
}
private static String streamToString(InputStream inputStream)
throws IOException {
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int readLen = 0;
while ((readLen = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, readLen);
}
byte[] retVal = outputStream.toByteArray();
return new String(retVal);
}