小编Tim*_*h93的帖子

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

如何在HTTP get请求的标头中使用apikey设置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 "";
    }
Run Code Online (Sandbox Code Playgroud)

编辑:使用下面的答案更改了代码但仍然收到错误消息:它无法实例化类型HttpURLConnection(url).我已经改变它但现在我必须覆盖3种方法(下面)

private static String download(String theUrl)
    {
        try {
            URL url = new …
Run Code Online (Sandbox Code Playgroud)

java android get http-headers

6
推荐指数
2
解决办法
4万
查看次数

标签 统计

android ×1

get ×1

http-headers ×1

java ×1