使用 Java HttpUrlConnection 发送带有令牌的 GET 请求
Posted
技术标签:
【中文标题】使用 Java HttpUrlConnection 发送带有令牌的 GET 请求【英文标题】:Send GET request with token using Java HttpUrlConnection 【发布时间】:2018-01-25 15:32:14 【问题描述】:我必须使用来自 Java 应用程序的基于令牌的身份验证的 RESTful Web 服务。我可以通过这种方式成功获取token:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public void getHttpCon() throws Exception
String POST_PARAMS = "grant_type=password&username=someusrname&password=somepswd&scope=profile";
URL obj = new URL("http://someIP/oauth/token");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json;odata=verbose");
con.setRequestProperty("Authorization",
"Basic Base64_encoded_clientId:clientSecret");
con.setRequestProperty("Accept",
"application/x-www-form-urlencoded");
// For POST only - START
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
// print result
System.out.println(response.toString());
else
System.out.println("POST request not worked");
但我找不到在获取请求中正确发送此令牌的方法。我正在尝试:
public StringBuffer getSmth(String urlGet, StringBuffer token) throws IOException
StringBuffer response = null;
URL obj = new URL(urlGet);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
String authString = "Bearer " + Base64.getEncoder().withoutPadding().encodeToString(token.toString().getBytes("utf-8"));
con.setRequestProperty("Authorization", authString);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
else
System.out.println("GET request not worked");
return response;
不起作用。任何解决此问题的帮助将不胜感激。
【问题讨论】:
【参考方案1】:您应该将令牌添加到请求网址:
String param = "?Authorization=" + token;
URL obj = new URL(urlGet + param);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
conn.setRequestMethod("GET");
或者,使用restTemplate
发送获取请求:
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + token);
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange(urlGet, HttpMethod.GET, request, String.class);
【讨论】:
【参考方案2】:解决了。除了令牌本身之外,服务器还返回一些额外的字符串。我所要做的就是从收到的答案中提取纯令牌并粘贴它而不进行任何编码: String authString = "Bearer" + pure_token;
【讨论】:
以上是关于使用 Java HttpUrlConnection 发送带有令牌的 GET 请求的主要内容,如果未能解决你的问题,请参考以下文章
Java 使用HttpURLConnection 设置头部 设置的Authorization不成功
java使用HttpURLConnection发送Post数据
java 使用原生HttpURLConnection发送post请求
我可以在使用 java 的 HttpUrlConnection 类的地方覆盖 Host 标头吗?