火币行情api使用java https调用问题

火币行情api使用java https调用问题

网络之后,在浏览器输入连接可以正常访问,但是用java okhttp调用则一直连接超时

如图:
这里写图片描述


通过java连接代码如下:

 // call api by endpoint.
     T call(String method, String uri, Object object, Map params,
               TypeReference ref) {
        ApiSignature sign = new ApiSignature();
        sign.createSignature(this.accessKeyId, this.accessKeySecret, method, API_HOST, uri, params);
        try {
            Request.Builder builder = null;
            if ("POST".equals(method)) {
                RequestBody body = RequestBody.create(JSON, JsonUtil.writeValue(object));
                builder = new Request.Builder().url(API_URL + uri + "?" + toQueryString(params));
                builder.addHeader("Content-Type", "application/json");
                builder.post(body);
            } else {
                builder = new Request.Builder().url(API_URL + uri + "?" + toQueryString(params));
                builder.addHeader("Content-Type", "application/x-www-form-urlencoded");
                builder.get();
            }
            if (this.assetPassword != null) {
                builder.addHeader("AuthData", authData());
            }
            builder.removeHeader("User-Agent").addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36");
            Request request = builder.build();
            Response response = client.newCall(request).execute();
            String s = response.body().string();
            return JsonUtil.readValue(s, ref);
        } catch (IOException e) {
            throw new ApiException(e);
        }
    }

代码来自火币官方API javademo 链接如下:
https://github.com/huobiapi/API_Docs/wiki/REST_api_reference

报错信息如下

API Error! err-code: java.net.SocketTimeoutException, err-msg: java.net.SocketTimeoutException: connect timed out
com.ruida.market.huobi.api.ApiException: java.net.SocketTimeoutException: connect timed out
    at com.ruida.market.huobi.api.ApiClient.call(ApiClient.java:459)
    at com.ruida.market.huobi.api.ApiClient.get(ApiClient.java:425)
    at com.ruida.market.huobi.api.ApiClient.trade(ApiClient.java:235)
    at com.ruida.market.huobi.api.Main.apiSample(Main.java:83)
    at com.ruida.market.huobi.api.Main.main(Main.java:40)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at okhttp3.internal.platform.Platform.connectSocket(Platform.java:124)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:187)
    at okhttp3.internal.connection.RealConnection.buildConnection(RealConnection.java:173)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:114)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:193)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:129)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:98)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
    at okhttp3.RealCall.execute(RealCall.java:60)
    at com.ruida.market.huobi.api.ApiClient.call(ApiClient.java:455)
    ... 4 more

你可能感兴趣的:(火币行情api使用java https调用问题)