您好,登錄后才能下訂單哦!
本篇文章為大家展示了java中怎么實現(xiàn)投放pythonsession功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
在pom.xml中加入相關(guān)依賴
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --><dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.2.2</version></dependency>
后面是相關(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();// //如果直接寫個類,里面寫saveFromResponse和loadForRequest,而不實現(xiàn)CookieJar接口,運行可以正常通過,但ide會顯紅,因為Bulider類的cookieJsr方法需要接受CookieJar類型的實例。鴨子類除了能節(jié)約代碼行數(shù),在可理解性 可讀性 多人合作性上面都不如接口規(guī)范,誰知道鴨子類里面要寫什么方法,除了寫代碼得人自己。 private class CookieJarManager implements CookieJar{ private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>(); @Override public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { cookieStore.put(url.host(), cookies); } @Override public List<Cookie> loadForRequest(HttpUrl url) { List<Cookie> cookies = cookieStore.get(url.host()); return cookies != null ? cookies : new ArrayList<Cookie>(){}; } } /** * @param url 要請求的url * @param paramsMap post的請求參數(shù) * @return post的返回結(jié)果 */ public String post(String url, HashMap<String, String > paramsMap){ FormBody.Builder formBodyBuilder = new FormBody.Builder(); Set<String> 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<String,String> 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中怎么實現(xiàn)投放pythonsession功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。