本篇文章為大家展示了java中怎么實(shí)現(xiàn)投放pythonsession功能,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供網(wǎng)站制作、成都做網(wǎng)站網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計(jì),成都響應(yīng)式網(wǎng)站建設(shè)公司,網(wǎng)頁設(shè)計(jì)師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:028-86922220
在pom.xml中加入相關(guān)依賴
com.squareup.okhttp3 okhttp 4.2.2
后面是相關(guān)的java代碼:
package com.puple.atto.datastructure;import java.text.MessageFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Set;import java.util.regex.*; import okhttp3.Cookie;import okhttp3.CookieJar;import okhttp3.FormBody;import okhttp3.HttpUrl;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class Session { private final OkHttpClient mOkHttpClient = new OkHttpClient.Builder().cookieJar(new CookieJarManager()).build();// //如果直接寫個(gè)類,里面寫saveFromResponse和loadForRequest,而不實(shí)現(xiàn)CookieJar接口,運(yùn)行可以正常通過,但ide會(huì)顯紅,因?yàn)锽ulider類的cookieJsr方法需要接受CookieJar類型的實(shí)例。鴨子類除了能節(jié)約代碼行數(shù),在可理解性 可讀性 多人合作性上面都不如接口規(guī)范,誰知道鴨子類里面要寫什么方法,除了寫代碼得人自己。 private class CookieJarManager implements CookieJar{ private final HashMap> cookieStore = new HashMap<>(); @Override public void saveFromResponse(HttpUrl url, List cookies) { cookieStore.put(url.host(), cookies); } @Override public List loadForRequest(HttpUrl url) { List cookies = cookieStore.get(url.host()); return cookies != null ? cookies : new ArrayList(){}; } } /** * @param url 要請(qǐng)求的url * @param paramsMap post的請(qǐng)求參數(shù) * @return post的返回結(jié)果 */ public String post(String url, HashMap paramsMap){ FormBody.Builder formBodyBuilder = new FormBody.Builder(); Set keySet = paramsMap.keySet(); for(String key:keySet) { String value = paramsMap.get(key); formBodyBuilder.add(key,value); } FormBody formBody = formBodyBuilder.build(); Request request = new Request .Builder() .post(formBody) .url(url) .build(); try (Response response = mOkHttpClient.newCall(request).execute()) { String respStr = response.body().string(); return respStr; }catch (Exception e){ e.printStackTrace(); return ""; } } public String get(String url) { final Request.Builder builder = new Request.Builder(); builder.url(url); final Request request = builder.build(); try (Response response = mOkHttpClient.newCall(request).execute()) { return response.body().string(); }catch (Exception e){ e.printStackTrace(); return ""; } } public static void test(){ Session ss=new Session(); String loginUrl="http://192.168.9.244:8080/RDCJWeb/login"; HashMap paramsMap = new HashMap<>() ; paramsMap.put("username","soft1"); paramsMap.put("password","soft1"); String respStr = ss.post(loginUrl,paramsMap); String res=ss.get("http://192.168.9.244:8080/RDCJWeb/static/dist/html/deviceWarn.html"); System.out.println(res); } public static void main(String[] args) { test(); } }
上述內(nèi)容就是java中怎么實(shí)現(xiàn)投放pythonsession功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱:java中怎么實(shí)現(xiàn)投放pythonsession功能
網(wǎng)站URL:
http://weahome.cn/article/pdhdho.html