package com.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.xml.bind.DatatypeConverter;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class restTest {
public static voidmain(String[] args) {
try {
DefaultHttpClient Client = newDefaultHttpClient();
HttpGet httpGet = newHttpGet("你的地址");
String encoding =DatatypeConverter.printBase64Binary("admin:admin".getBytes("UTF-8"));
httpGet.setHeader("Authorization", "Basic " +encoding);
HttpResponse response = Client.execute(httpGet);
System.out.println("response =" + response);
BufferedReader breader = newBufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder responseString = newStringBuilder();
String line = "";
while ((line = breader.readLine()) !=null) {
responseString.append(line);
}
breader.close();
String repsonseStr =responseString.toString();
System.out.println("repsonseStr =" + repsonseStr);
} catch (IOException e) {
e.printStackTrace();
}
}
}
JAVA模拟浏览器调用rest api接口
最新推荐文章于 2024-07-03 16:27:46 发布