添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
霸气的蛋挞  ·  WPF ...·  1 年前    · 
机灵的草稿本  ·  Use labels in an ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How can i set an x-api-key with the apikey in the header of a HTTP get request. 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). I have changed it but now i have to override 3 methods (below)

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.

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);
                Thanks for your answer, but after i paste this code into my code. I have to override 1 method (connect). Am i supposed to add  ucon.addRequestProperty("x-api-key", apiKey); inside this method? or is it to late and has there already been an connection established?
– Timmeeh93
                Oct 24, 2014 at 16:54
                I've updated my answer- you should be using HttpURLConnection, as URLConnection doesn't necessarily use HTTP and doesn't have an implementation of connect() (whereas HttpURLConnection does).
– Bryan Herbst
                Oct 24, 2014 at 16:56
                But i'm getting an error saying that it couldn't instantiate the type HttpURLConnection(url). I'll update my post above what eclipse is saying i have to do.
– Timmeeh93
                Oct 24, 2014 at 17:02
                My apologies, I forgot HttpURLConnection is also abstract. I updated my answer with a better solution that uses an actual HTTP request instead of just a URL connection.
– Bryan Herbst
                Oct 24, 2014 at 17:17

The use of Apache HttpClient is now discouraged. You can check it here: Android 6.0 Changes

You should better use URLConnection and cast to HttpURLConnection:

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.