国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java 模擬cookie登陸簡單操作示例

瀏覽:145日期:2022-09-05 08:00:38

本文實(shí)例講述了Java 模擬cookie登陸簡單操作。分享給大家供大家參考,具體如下:

最近在做將禪道上的功能接口做到手機(jī)端,在做登陸的時(shí)候,看了禪道的源碼,是由cookie來登陸,所以要做一個(gè)模擬cookie登陸的接口,將拿到的cookie放到每次接口請求的頭部中去,就可以正常訪問了。

import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;/** * @Author: jljiang * @Description:Java 模擬cookie登陸 * @Date: Created in 2019/1/16 15:14 */public class ImitateLoginController { public static void main(String args[]) throws Exception { //登陸接口地址 String loginStr = 'http://zenta.51fb.com/index.php?m=user&f=login'; /** * 首先要和URL下的URLConnection對(duì)話。 URLConnection可以很容易的從URL得到。比如: // Using * java.net.URL and //java.net.URLConnection */ URL url = new URL(loginStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), 'GBK'); //其中的account和password可以通過控制臺(tái)去查看,或者看頁面html去查看 out.write('account=you-user-name&password=you-password');// remember to clean up out.flush(); out.close();// 取得cookie,使用該cookie放在頭部就可以訪問其他需要登陸才可以訪問的接口了 String cookieVal = connection.getHeaderField('Set-Cookie');/*------------------------------------訪問其他接口-------------------------------------------------*/ String otherUrl = 'http://zenta.51fb.com/index.php?m=bug&f=browse'; url = new URL(otherUrl); HttpURLConnection otherConnection = (HttpURLConnection) url.openConnection(); if(cookieVal != null){ otherConnection.setRequestProperty('Cookie',cookieVal); } otherConnection.connect(); InputStream urlStream = otherConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlStream)); String content = null; StringBuilder total = new StringBuilder(); while ((content = bufferedReader.readLine()) != null) { total.append(content); } bufferedReader.close(); System.out.println(content); }}

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 喀什市| 阳原县| 烟台市| 南岸区| 呼伦贝尔市| 石河子市| 临湘市| 石台县| 阜阳市| 沙雅县| 蒙阴县| 商丘市| 保定市| 高陵县| 穆棱市| 弥勒县| 长岛县| 德昌县| 宁陵县| 巴林右旗| 和政县| 阳曲县| 青铜峡市| 华池县| 南川市| 青川县| 原平市| 常德市| 鄂伦春自治旗| 宜君县| 资溪县| 娄烦县| 抚远县| 宜丰县| 灵台县| 红河县| 汝城县| 双城市| 筠连县| 卢湾区| 渝北区|