Java 如何在 HTTP get 请求的头部设置 X-Api-Key
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26552149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set X-Api-Key in the header of HTTP get request
提问by Timmeeh93
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:
如何在 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 "";
}
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)
编辑:使用下面的答案更改了代码,但仍然收到错误消息:它无法实例化 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 "";
}
采纳答案by Bryan Herbst
Instead of using a URLConnection
, you should be using an HttpClient
to make a request.
URLConnection
您应该使用 anHttpClient
来发出请求,而不是使用。
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);
回答by juanlugm
The use of Apache HttpClient is now discouraged. You can check it here: Android 6.0 Changes
现在不鼓励使用 Apache HttpClient。您可以在这里查看:Android 6.0 Changes
You should better use URLConnection and cast to HttpURLConnection:
您应该更好地使用 URLConnection 并转换为 HttpURLConnection:
HttpURLConnection huc= (HttpURLConnection) url.openConnection();
huc.setRequestProperty("x-api-key","value");