栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

linkedin中Http请求中的授权标头

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linkedin中Http请求中的授权标头

您可以使用该

apache.http
库。您不需要一些OAuth库或其他任何内容。OAuth协议非常易于处理,您可以使用普通的HTTP请求来执行。这是
apache-http 库的示例。

[编辑] 我更改了代码以提供完整的示例,说明如何使用这些库。

import java.io.IOException;import org.apache.http.HttpResponse;import org.apache.http.ParseException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.util.EntityUtils;public class OAuthConnect {    public static void main(String[] args) {        try { HttpClient httpclient = HttpClientBuilder.create().build();  // the http-client, that will send the request HttpGet httpGet = new HttpGet("");   // the http GET request httpGet.addHeader("Authorization", "Bearer AQXdSP_W41_UPs5ioT_t8HESyODB4FqbkJ8LrV_5mff4gPODzOYR"); // add the authorization header to the request HttpResponse response = httpclient.execute(httpGet); // the client executes the request and gets a response int responseCode = response.getStatusLine().getStatusCode();  // check the response pre switch (responseCode) {     case 200: {          // everything is fine, handle the response         String stringResponse = EntityUtils.toString(response.getEntity());  // now you have the response as String, which you can convert to a JSonObject or do other stuff         break;     }     case 500: {         // server problems ?         break;     }     case 403: {         // you have no authorization to access that resource         break;     } }        } catch (IOException | ParseException ex) { // handle exception        }    }}

在这里,您可以找到

jar
可以添加为库的文件:

Apache HTTP核心v 4.3.3


Apache HTTP客户端v
4.3.6

您也可以从Apache页面下载jar

如您所见,这些库为您提供了处理访问API(

GET POSTDELETE..
)可能需要的所有请求的所有功能。您可以更改标题并处理您收到的任何内容作为响应。我知道它看起来很复杂,但是有了它,您将可以完全控制OAuth请求,而不必依赖任何库。

[另一个编辑]
从Apache页面下载zip文件时,需要将其解压缩,所需

jar
文件位于该
lib
文件夹中。

httpcomponents-core-4.3.3   +-examples   +-lib      +-commons-cli-1.2.jar      +-httpcore-4.3.3.jar   <-- this one you need      +-httpcore-ab-4.3.3.jar     ...

和相同的

httpClient

httpcomponents-client-4.3.6   +-examples   +-lib      +- commons-prec-1.6.jar      +- ...      +- httpclient-4.3.6.jar  <-- this one      +- ...


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443486.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号