Android Java Bing搜索Api HTTP错误401

时间:2016-01-03 15:32:53

标签: java android api search bing

我的天蓝帐号无法获得有效的同意。 始终出现Http Error 401 - >虚假认证 几个小时后,我无法找到解决方案。

    AsyncTask<Void, Void, Void> task = new AsyncTask <Void, Void, Void> () {

        protected Void doInBackground(Void... args) {
            String searchText = "Android";
            searchText = searchText.replace(" ", "%20");
            String accountKey = "myazureaccountkey";
            accountKey = accountKey.replace("+", "%2B");

            byte[] accountAccess = ("accountKey:" + accountKey).getBytes();
            byte[] accountAccessBytes = Base64.encode(accountAccess, Base64.DEFAULT);
            String accountAccessString = new String(accountAccessBytes);

            final String defaultURL = "https://api.datamarket.azure.com/Bing/Search/v1/Image?Query=%27";
            InputStream inputStream = null;

            try {
                URL url = new URL(defaultURL + searchText + "%27" + "&$format=Atom");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Authorization", "Basic " + accountAccessString);

                if (conn.getResponseCode() >= 400) {
                    inputStream = conn.getErrorStream();
                } else {
                    inputStream = conn.getInputStream();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    };

    task.execute();

0 个答案:

没有答案