【发布时间】:2017-10-18 05:27:07
【问题描述】:
我正在使用 okhttp 从 url 下载字符串。这是代码。
public static void main(String[] args) throws InterruptedException {
String jsonString = downloadJsonString("https://api.korbit.co.kr/v1/ticker/detailed?currency_pair=btc_krw");
System.out.println(jsonString);
}
String downloadJsonString (String url) throws IOException {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
}
结果是这样的:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Request blocked.
<BR clear="all">
<HR noshade size="1px">
<PRE>Generated by cloudfront (CloudFront)
Request ID: jIs6PvNF01BMsQrynj4pk8eBXuQW-n-Q02Tav0SQghSZ7TzZOFfrAg==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>
如果我将 url 放入浏览器,它就可以正常工作。回复如下:
{"timestamp":1507942765556,"last":"6355000","bid":"6355000","ask":"6358500","low":"6310000","high":"6949000","volume":"12329.815314799943534093","change":"0","changePercent":"0"}
如何解决?
【问题讨论】:
标签: java authentication url httprequest okhttp