黄金价格数据API API提供方:https:www。nowapi。com 国际金价实时(盎司):https:www。nowapi。comapifinance。gsgold 国际金价实时(盎司) 作用:查询上海黄金交易所黄金价格、国际公制金价、国际贵金属期货价、工行纸黄金等。 测试示例:http:api。k780。com?appfinance。goldpricegoldid1201appkey10003signb59bc3ef6191eb9f747dd4e83c99f2a4formatjson(示例中sign会不定期调整) 调用参数: 返回格式:{result:{dtList:{1201:{sellprice:1716。73,highprice:1719。2,goldid:1201,uptime:2022072213:58:01,varietynm:现货黄金,variety:XAU,changemargin:0。14,yesyprice:1718。65,buyprice:1716。3,changeprice:2。35,openprice:1717。84,lowprice:1712。77,lastprice:1716。3}},dtQuery:1201,dtCount:1},success:1}调用代码案例 pom。xmldependenciesdependencygroupIdcn。hutoolgroupIdhutoolallartifactIdversion5。5。9versiondependencydependencygroupIdorg。apache。httpcomponents。client5groupIdhttpclient5artifactIdversion5。1versiondependencydependencygroupIdorg。apache。httpcomponents。client5groupIdhttpclient5fluentartifactIdversion5。0。3versiondependencydependencies 调用方式1:importorg。apache。commons。codec。Cimportorg。apache。hc。client5。http。classic。methods。HttpPimportorg。apache。hc。client5。http。entity。UrlEncodedFormEimportorg。apache。hc。client5。http。impl。classic。CloseableHttpCimportorg。apache。hc。client5。http。impl。classic。CloseableHttpRimportorg。apache。hc。client5。http。impl。classic。HttpCimportorg。apache。hc。core5。http。HttpEimportorg。apache。hc。core5。http。NameValuePimportorg。apache。hc。core5。http。io。entity。EntityUimportorg。apache。hc。core5。http。message。BasicNameValuePimportjava。util。ArrayLimportjava。util。LpublicclassHttpClientClassicApp{publicstaticvoidmain(String〔〕args)throwsException{CloseableHttpClienthttpClientHttpClients。createDefault();HttpPosthttpPostnewHttpPost(http:api。k780。com);ListNameValuePairnvpsnewArrayList();nvps。add(newBasicNameValuePair(app,finance。goldprice));nvps。add(newBasicNameValuePair(goldid,1201));nvps。add(newBasicNameValuePair(appkey,10003));nvps。add(newBasicNameValuePair(sign,b59bc3ef6191eb9f747dd4e83c99f2a4));nvps。add(newBasicNameValuePair(format,json));httpPost。setEntity(newUrlEncodedFormEntity(nvps,Charsets。UTF8));CloseableHttpResponseresponsehttpClient。execute(httpPost);System。out。println(response。getCode()response。getReasonPhrase());HttpEntityentityresponse。getEntity();StringcontentEntityUtils。toString(entity);System。out。println(content);EntityUtils。consume(entity);}} 调用方式2:importcn。hutool。json。JSONOimportcn。hutool。json。JSONUimportorg。apache。hc。client5。http。async。methods。;importorg。apache。hc。client5。http。config。RequestCimportorg。apache。hc。client5。http。impl。async。CloseableHttpAsyncCimportorg。apache。hc。client5。http。impl。async。HttpAsyncCimportorg。apache。hc。client5。http。impl。nio。PoolingAsyncClientConnectionMimportorg。apache。hc。client5。http。impl。nio。PoolingAsyncClientConnectionManagerBimportorg。apache。hc。core5。concurrent。FutureCimportorg。apache。hc。core5。http。Mimportorg。apache。hc。core5。http。message。StatusLimportorg。apache。hc。core5。pool。PoolConcurrencyPimportorg。apache。hc。core5。reactor。IOReactorCimportorg。apache。hc。core5。util。TimeVimportorg。apache。hc。core5。util。Timportjava。util。concurrent。Fimportjava。util。concurrent。TimeUpublicclassHttpClient5App{publicstaticvoidmain(String〔〕args){异步应用配置IOReactorConfigioReactorConfigIOReactorConfig。custom()。setSoTimeout(Timeout。ofMilliseconds(250))超时时间。setSelectInterval(TimeValue。ofMilliseconds(50))1。2。build();setSoTimeout()连接上一个url,获取response的返回等待时间setSelectInterval()NIO中设置select的间隔客户端连接配置PoolingAsyncClientConnectionManagerbuildPoolingAsyncClientConnectionManagerBuilder。create()。setPoolConcurrencyPolicy(PoolConcurrencyPolicy。LAX)2。1。setMaxConnPerRoute(6)。build();2。2poolConcurrencyPolicy()STRICT模式通过加锁的方式对,LAX通过cas的方式宽松计数设置STRICT,会使用StrictConnPool实现类;设置LAX,会使用LaxConnPool实现类maxConnPerRoute每个route最多能有多少个connection创建客户端CloseableHttpAsyncClientclientHttpAsyncClients。custom()。setIOReactorConfig(ioReactorConfig)。setConnectionManager(build)。disableAutomaticRetries()。build();automaticRetriesDisabled关闭自动重试client。start();Stringurihttp:api。k780。SimpleHttpRequesthttpRequestSimpleHttpRequest。create(Method。GET。name(),uri);finalSimpleHttpRequestrequestSimpleRequestBuilder。copy(httpRequest)。addParameter(app,finance。goldprice)。addParameter(goldid,1201)。addParameter(appkey,10003)。addParameter(sign,b59bc3ef6191eb9f747dd4e83c99f2a4)。addParameter(format,json)。build();request。setHeader(Host,api。k780。com);RequestConfigconfigRequestConfig。copy(RequestConfig。DEFAULT)。setConnectTimeout(150,TimeUnit。MILLISECONDS)。setConnectionRequestTimeout(200,TimeUnit。MILLISECONDS)。setResponseTimeout(100,TimeUnit。MILLISECONDS)。build();request。setConfig(config);longstartSystem。currentTimeMillis();finalFutureSimpleHttpResponsefutureclient。execute(SimpleRequestProducer。create(request),SimpleResponseConsumer。create(),newFutureCallbackSimpleHttpResponse(){Overridepublicvoidcompleted(finalSimpleHttpResponseresponse){System。out。println(requestnewStatusLine(response));System。out。println(response。getBody());}Overridepublicvoidfailed(finalExceptionex){System。out。println(requestex);}Overridepublicvoidcancelled(){System。out。println(requestcancelled);}});时间计算try{SimpleHttpResponsesimpleHttpResponsefuture。get();JSONObjectjsonObjectJSONUtil。parseObj(simpleHttpResponse。getBodyText());System。out。println(bodyjsonObject);longendSystem。currentTimeMillis();System。out。println(时间差:(endstart));}catch(Exceptione){longendSystem。currentTimeMillis();System。out。println(时间差:(endstart));System。out。println(e);}线程等待try{Thread。sleep(10000);}catch(InterruptedExceptione){e。printStackTrace();}}}