通过HTTP将请求发送到graphQL API网站WikiCSMoney并获取Java中的JSON响应

问题描述

我需要向服务WikiCsMoney发出请求,并以json格式获取响应。我提出了以下要求

String uri = "https://wiki.cs.money/graphql?query=query%20skin($id:%20ID!)%20{%20%20skin(id:%20$id)%20{%20%20%20%20price_trader_log%20{%20%20%20%20%20%20name%20%20%20%20%20%20values%20{%20%20%20%20%20%20%20%20price_trader_new%20%20%20%20%20%20%20%20time%20%20%20%20%20%20}%20%20%20%20}%20%20}}&operationName=skin&variables=%7B%22id%22%3A%22usp-s-torque%22%7D"

如果我通过浏览器的命令行或通过邮递员手动执行此请求,则它将返回我需要的答案。 enter image description here

我正在以适当的方式提出请求

public  String sendRequestToWiki(String PROXY_HOST) throws IOException {
        String uri = "https://wiki.cs.money/graphql?query=query%20skin($id:%20ID!)%20{%20%20skin(id:%20$id)%20{%20%20%20%20price_trader_log%20{%20%20%20%20%20%20name%20%20%20%20%20%20values%20{%20%20%20%20%20%20%20%20price_trader_new%20%20%20%20%20%20%20%20time%20%20%20%20%20%20}%20%20%20%20}%20%20}}&operationName=skin&variables=%7B%22id%22%3A%22usp-s-torque%22%7D";
        //String encodedURI = URLEncoder.encode(uri,StandardCharsets.UTF_8.toString());
        System.out.println(uri);
        String username = "username";
        String password = "password";
        HttpClient client = new HttpClient();
        HttpMethod method = new PostMethod(uri);

        HostConfiguration config = client.getHostConfiguration();
        config.setProxy(PROXY_HOST,PROXY_PORT);

        Credentials credentials = new UsernamePasswordCredentials(username,password);
        AuthScope authScope = new AuthScope(PROXY_HOST,PROXY_PORT);

        client.getState().setProxyCredentials(authScope,credentials);
        client.executeMethod(method);

        String responseBody = method.getResponseBodyAsString();
        JSONObject skinInfo = new JSONObject(responseBody);
        JSONObject itemInf = skinInfo.getJSONObject("skin");
        JSONArray priceTraderLog = itemInf.getJSONArray("price_trader_log");
        String nameItem = priceTraderLog.getJSONObject(0).getString("name");
        return nameItem;
}

但是我收到以下异常消息

Exception in thread "main" java.lang.IllegalArgumentException: Invalid uri 'https://wiki.cs.money/graphql?query=query%20skin($id:%20ID!)%20{%20%20skin(id:%20$id)%20{%20%20%20%20price_trader_log%20{%20%20%20%20%20%20name%20%20%20%20%20%20values%20{%20%20%20%20%20%20%20%20price_trader_new%20%20%20%20%20%20%20%20time%20%20%20%20%20%20}%20%20%20%20}%20%20}}&operationName=skin&variables=%7B%22id%22%3A%22usp-s-torque%22%7D': Invalid query
    at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222)
    at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
    at noSteam.SteamPage.sendRequestToWiki(SteamPage.java:526)
    at noSteam.MainTester.main(MainTester.java:25)

有人可以帮我解决这个问题吗,因为如果您手动发送请求,那么我们会得到正确的答案,这意味着Yuri是正确的,也许我使用了错误的方法来执行请求。预先感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...