av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

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

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

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

最近在做將禪道上的功能接口做到手機端,在做登陸的時候,看了禪道的源碼,是由cookie來登陸,所以要做一個模擬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對話。 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可以通過控制臺去查看,或者看頁面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); }}

更多關于java算法相關內容感興趣的讀者可查看本站專題:《Java數據結構與算法教程》、《Java操作DOM節點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設計有所幫助。

標簽: Java
相關文章:
主站蜘蛛池模板: 97超碰人人草 | 成人高清在线 | 成人国产免费视频 | 国产精品久久久久久网站 | 国产精品美女久久久久久免费 | 欧美一级三级在线观看 | 亚洲福利在线视频 | 一区二区视频在线 | 国产欧美一区二区三区久久手机版 | 亚洲视频欧美视频 | 婷婷精品 | 日韩中文字幕区 | 亚洲久在线 | 特黄毛片视频 | 精品国产乱码久久久久久图片 | 亚州一区二区三区 | 在线精品国产 | 羞羞视频免费观 | 完全免费在线视频 | 日本aaaa| 天天射天天操天天干 | 99精品欧美一区二区蜜桃免费 | 国产欧美精品一区二区 | 国产传媒视频在线观看 | 国产超碰人人爽人人做人人爱 | 国产在线观看一区二区 | 日韩久久精品 | 亚洲精品1区| 日韩欧美国产精品一区二区 | 91精品国产777在线观看 | 成人性视频在线播放 | 亚洲天堂影院 | 国产精品久久久久久久久久久免费看 | 国产精品一区在线 | 99国产精品99久久久久久 | 欧美一级欧美三级在线观看 | 一二三四av| 精品国产欧美 | 午夜99| 精品区 | 亚洲欧美中文日韩在线 |