微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

授权标头在Android中与HTTPUrlConnection间歇性失败

如何解决授权标头在Android中与HTTPUrlConnection间歇性失败

我在Android中使用HttpUrlConnection实现了应用程序和聊天机器人服务器之间的通信。事实是,在95%的时间内,一切正常。

聊天机器人服务器返回代码200的其他5%的次数,但拒绝与应用程序中的用户聊天。 因此,代码200告诉我通信可以正常进行,直到应用程序可以向聊天机器人服务器发送请求以开始与机器人的对话为止。服务器以状态200和响应进行响应,该响应指示是否允许用户发送带有进一步请求的与机器人的对话。

5%的情况是Authorization标头中的“ Something”(oauth标头,使用者密钥,会话等)错误,并且聊天机器人服务器使用响应代码200和消息通知应用程序说:“错误40发生了,无法与漫游器进行对话”。 (当Authorization标头看起来不像这样时,将返回错误40:

oauth_consumer_key="FjdN7KJPRXEGpAPhabc123b9XobQsLuUHuHL",oauth_nonce="-abc1238625690508",oauth_signature="1abc4fAlCAF033OmpuS6FpWXo%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1364453",oauth_version="1.0"

顺便说一句,正如您从我的HTTPUrlConnection代码下面看到的那样,这些值均未设置,因此我假设在幸运的成功条件下,将设置认值:

@Override
        protected Void doInBackground(String... params) {
            try {
                this.url = new URL(getBackendEndpoint());
                HttpURLConnection urlConnection = null;
                bodyData = this.params.getBytes(StandardCharsets.UTF_8);
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setDoOutput(true);
                urlConnection.setRequestMethod("POST");
                urlConnection.setInstanceFollowRedirects(false);
                urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                urlConnection.setRequestProperty("charset","utf-8");
                urlConnection.setRequestProperty("Connection","Keep-Alive");
                urlConnection.setRequestProperty("Cache-Control","no-cache");
                urlConnection.setRequestProperty("Content-Length",Integer.toString(bodyData.length));
                urlConnection.setRequestProperty("Accept-Encoding","UTF-8");
                urlConnection.setUseCaches(false);
                urlConnection.setConnectTimeout(10000);
                urlConnection.setReadTimeout(10000);

                String baseAuthStr = ANDROID_CONSUMER_KEY + ":" + ANDROID_CONSUMER_SECRET;
                urlConnection.addRequestProperty("Authorization","Basic " + baseAuthStr);

                try (DataOutputStream wr = new DataOutputStream(urlConnection.getoutputStream())) {
                    wr.write(bodyData);
                }

                urlConnection.connect();

                // Responses from the server (code and message)
                InputStream inputStream;
                int status = urlConnection.getResponseCode();

                if (status != HttpURLConnection.HTTP_OK) {
                    success = false;
                    inputStream = urlConnection.getErrorStream();
                } else {
                    success = true;
                    inputStream = urlConnection.getInputStream();
                }

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String inputLine;
                result="";
                while ((inputLine = reader.readLine()) != null) {
                    result += inputLine;
                }
                reader.close();

                loge("KB URLCONNECTION,getResponseCode: " + String.valueOf(status)); //This is most surely always 200
                loge("KB URLCONNECTION,result: " + result); //Here is where Error 40 is informed by the Backend
                urlConnection.disconnect();
                if (result.isEmpty()) {
                    result = "{}";
                }
            } catch (MalformedURLException | ProtocolException e) {
                handleException(e);
                e.printstacktrace();
            } catch (IOException e) {
                e.printstacktrace();
                handleException(e);
            }

            responseHandler.handleResponse(success,result);
            return null;
        }

那么为什么HTTPURLConnection有时只会失败? 我可以打印/记录成功请求的标题并将它们与错误请求进行比较吗? 在HTTPUrlConnection中设置oauth授权标头的正确方法是什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。