1

我目前正在尝试使用以下存储库中的给定代码实现 Steam Web API -> https://github.com/Overv/SteamWebAPI/blob/master/SteamAPISession.cs到我的 Android 应用程序中,并且我得到不同的异常取决于使用给定的 wep api ip ( 63.228.223.110 ) 或地址 ( https://api.steampowered.com/ ) 本身。

在建立与 Web api 的连接时,我的代码在给定方法中实际上如下所示:

    private String steamRequest( String get, String post ) {

    final int MAX_RETRIES = 3;
    int numberOfTries = 0;

    HttpsURLConnection request = null;

    while(numberOfTries < MAX_RETRIES) {

        if (numberOfTries != 0) {
            Log.d(TAG, "Retry -> " + numberOfTries);
        }

        try {
            request = (HttpsURLConnection) new URL("https://api.steampowered.com/" + get).openConnection(); //or 63.228.223.110/ ???

            SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            String host = "api.steampowered.com";
            int port = 443;
            int header = 0;

            socketFactory.createSocket(new Socket(host, port), host, port, false);
            request.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
            });
            request.setSSLSocketFactory(socketFactory);
            request.setDoOutput(false);
            //            request.setRequestProperty("Host", "api.steampowered.com:443");
            //            request.setRequestProperty("Protocol-Version", "httpVersion");
            request.setRequestProperty("Accept", "*/*");
            request.setRequestProperty("Accept-Encoding", "gzip, deflate");
            request.setRequestProperty("Accept-Language", "en-us");
            request.setRequestProperty("User-Agent", "Steam 1291812 / iPhone");
            request.setRequestProperty("Connection", "close");

            if (post != null) {
                byte[] postBytes;

                try {
                    request.setRequestMethod("POST");

                    postBytes = post.getBytes("US-ASCII");

                    //                    request.setRequestProperty("Content-Length", Integer.toString(postBytes.length));
                    request.setFixedLengthStreamingMode(postBytes.length);
                    request.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    PrintWriter out = new PrintWriter(request.getOutputStream());
                    out.print(post);
                    out.close();

                    //                    DataOutputStream requestStream = new DataOutputStream(request.getOutputStream());
                    ////                    OutputStreamWriter stream = new OutputStreamWriter(request.getOutputStream());
                    ////                    stream.write(postBytes, 0, postBytes.length);
                    //                    requestStream.write(postBytes, 0, postBytes.length);
                    //                    requestStream.close();

                    message++;

                } catch (ProtocolException e) {
                    e.printStackTrace();

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                int statusCode = request.getResponseCode();
                InputStream is;
                Log.d(TAG, "The response code of the status code is" + statusCode);
                if (statusCode != 200) {
                    is = request.getErrorStream();
                    Log.d(TAG, String.valueOf(is));
                }

                //            String src = null;
                //            OutputStreamWriter out = new OutputStreamWriter(request.getOutputStream());
                //            out.write(src);
                //            out.close();

                Scanner inStream = new Scanner(request.getInputStream());
                String response = "";
                while (inStream.hasNextLine()) {
                    response += (inStream.nextLine());
                }

                //                String src;
                //                BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
                //                StringBuilder builder = new StringBuilder();
                //                while ((src = in.readLine()) != null) {
                //                    builder.append(src);
                //                }

                //                String jsonData = builder.toString();
                Log.d(TAG, response); //jsonData

                //                in.close();

                return response; //jsonData

            } catch (MalformedURLException ex) {
                Toast.makeText(getActivity(), ex.toString(), Toast.LENGTH_LONG).show();
                ex.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (request != null) {
                request.disconnect();
            }
        }

        numberOfTries++;
    }
    Log.d(TAG, "Max retries reached. Giving up on connecting to Steam Web API...");
    return null;
}

使用https://api.steampowered.com/作为 url 时发生以下异常:

W/System.err:java.io.FileNotFoundException:https : //api.steampowered.com/ISteamOAuth2/GetTokenWithCredentials/v0001 D/OptionsFragment:登录失败!

我真的对这些问题进行了尝试和研究,但我无法找到解决方案。如果有人对帮助解决这些异常有很好的提示,请告诉我!

编辑: 我研究了更多!

不应该使用https://partner.steamgames.com/documentation/webapi#creating直接 IP 地址,因此只能使用 DNS 名称本身(为了进一步说明,请查看给定链接)。因此,在 API 接口列表本身上查找 -> http://api.steampowered.com/ISteamWebAPIUtil/GetSupportedAPIList/v0001/?format=json似乎没有一个给定的接口与一个名为 ISteamOAuth2/GetTokenWithCredentials 的方法。 (或者不再!)只有 ISteamUserAuth 和 ISteamUserOAuth 似乎处理身份验证的东西。

如果我应该获得有效的连接并使用 steam web api 进行处理,我将再次更新这篇文章。

干杯!

4

0 回答 0