其实查了很多文章,介绍了如何设置授权,自我感觉不太清楚。 自己思考并实地测试后,感觉最后是easy!
1 .首先让我们看看它是如何在postman上操作的。 添加授权是徒劳地为了身份认证。 所以,后面需要的值一般是token
可以通过图像获得。 在postman中设置授权时,请将其放在标题信息的位置。 所以,要把这个东西放进程序里,我想应该怎么做。
在设置授权之前,通过参考上面的参数Content-Type,java可以知道应该在哪里设置!
使用Header传递授权验证参数
把衣服放进去
代码部分:
/** * Post请求发送JSON数据。 {'name':'zhangsan ',' pwd':'123456'} *参数1 :请求Url *参数2 :请求的JSON *参数3 :回调*/publicstation stringjs ) )请求的requestbody requestbody=requestbody.create (media type.parse (application/JSON ); charset=utf-8 ' ),jsonParams; request request=new request.builder (.URL ).header ) ' authorization ',' parseconfigfiles.getpropertvalue response response try { okhttpclientokhttpclient=newokhttpclient (; response=ok http client.new call (request ).execute ); int status=response.code (; response.is successful () (returnJSONobject.parseobject ) response.body (.string ) ); }catch(exceptione ) logger.error ),ExceptionUtils.getStackTrace(e ) e ); (finally(if ) response!=空值(response.close ) ); } } return responseBody; }注:在本章中
. header('authorization ',' parseconfigfiles.getpropertvalue ) ' tokeninfo ' ' )
的parseconfigfiles.getpropertvalue (token info ) )这是一个获取我的token值的表达式,在这里可以根据自己的实际情况进行处理,直接放入token和其他认证字符串!