import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpGetDemo {
public static void main(String[] args) {
HttpGetDemo h = new HttpGetDemo();
ReadByGet r = h.new ReadByGet();
r.start();
}
class ReadByGet extends Thread{
HttpClient client = HttpClients.createDefault();
@Override
public void run() {
HttpGet get = new HttpGet("http://fanyi.youdao.com/openapi.do?keyfrom=huosensen&key=1871355394&type=data&doctype=xml&version=1.1&q=hello");
try {
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity, "utf-8");
System.out.println(str);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
本文介绍了一个使用Java实现的简单HTTP GET请求示例。通过调用有道翻译API获取翻译结果,展示了如何创建HttpClient实例、构造GET请求并解析响应内容。
3144

被折叠的 条评论
为什么被折叠?



