简体   繁体   English

如何在HTTP get请求的标头中设置X-Api-Key

[英]How to set X-Api-Key in the header of HTTP get request

How can i set an x-api-key with the apikey in the header of a HTTP get request. 如何在HTTP get请求的标头中使用apikey设置x-api-key。 I have tried something but it seems it doesnt work. 我尝试了一些东西,但似乎它不起作用。 Here is my code: 这是我的代码:

    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 "";
    }

EDIT: Changed the code with the answer below but still getting an error message: it couldn't instantiate the type HttpURLConnection(url). 编辑:使用下面的答案更改了代码但仍然收到错误消息:它无法实例化类型HttpURLConnection(url)。 I have changed it but now i have to override 3 methods (below) 我已经改变它但现在我必须覆盖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 "";
    }

Instead of using a URLConnection , you should be using an HttpClient to make a request. 您应该使用HttpClient来发出请求,而不是使用URLConnection

A simple example might look like this: 一个简单的例子可能如下所示:

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

The use of Apache HttpClient is now discouraged. 现在不鼓励使用Apache HttpClient。 You can check it here: Android 6.0 Changes 您可以在此处查看: Android 6.0更改

You should better use URLConnection and cast to HttpURLConnection: 你最好使用URLConnection并强制转换为HttpURLConnection:

HttpURLConnection huc= (HttpURLConnection) url.openConnection();
huc.setRequestProperty("x-api-key","value");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM