如何在HTTP GET请求的头部中设置X-Api-Key

13

如何在HTTP GET请求的标头中使用API密钥设置x-api-key。我已经尝试了一些东西,但似乎它不起作用。

这是我的代码:

    private static String download(String theUrl)
    {
        try {
            URL url = new URL(theUrl);

            URLConnection ucon = url.openConnection();

            ucon.addRequestProperty("x-api-key", apiKey);

            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current;
            while ((current = bis.read()) != -1)
            {
                baf.append((byte) current);
            }

            return new String (baf.toByteArray());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

编辑: 根据下面的答案更改了代码,但仍然收到错误消息:无法实例化类型HttpURLConnection(url)。我已经进行了更改,但现在我必须覆盖3个方法(如下)

private static String download(String theUrl)
    {
        try {
            URL url = new URL(theUrl);

            URLConnection ucon = new HttpURLConnection(url) {

                @Override
                public void connect() throws IOException {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean usingProxy() {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public void disconnect() {
                    // TODO Auto-generated method stub

                }
            };
            ucon.addRequestProperty("x-api-key", apiKey);
            ucon.connect();

            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current;
            while ((current = bis.read()) != -1)
            {
                baf.append((byte) current);
            }

            return new String (baf.toByteArray());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
2个回答

15

不应该使用URLConnection,而应该使用HttpClient来发起请求。

一个简单的例子可能是这样的:

HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(theUrl);
request.addHeader("x-api-key", apiKey);
HttpResponse response = httpclient.execute(request);

谢谢您的回答,但是在我将这段代码粘贴到我的代码后,我必须覆盖一个方法(connect)。我应该在这个方法里添加ucon.addRequestProperty("x-api-key", apiKey);吗?还是已经建立了连接,现在为时已晚? - Timmeeh93
我更新了我的答案- 你应该使用 HttpURLConnection,因为 URLConnection 并不一定使用 HTTP,并且没有 connect() 的实现(而 HttpURLConnection 则有)。 - Bryan Herbst
但是我收到了一个错误,说它无法实例化类型HttpURLConnection(url)。 我会更新我的帖子,告诉大家Eclipse要求我做什么。 - Timmeeh93
非常抱歉,我忘记了HttpURLConnection也是抽象的。我更新了我的答案,提供了一个更好的解决方案,使用实际的HTTP请求而不仅仅是URL连接。 - Bryan Herbst

7
现在不再建议使用Apache HttpClient。您可以在此处检查:Android 6.0更改 您最好使用URLConnection并将其转换为HttpURLConnection:
HttpURLConnection huc= (HttpURLConnection) url.openConnection();
huc.setRequestProperty("x-api-key","value");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接