您好,登錄后才能下訂單哦!
本篇文章為大家展示了java中怎么實現投放pythonsession功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
在pom.xml中加入相關依賴
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --><dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.2.2</version></dependency>
后面是相關的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,而不實現CookieJar接口,運行可以正常通過,但ide會顯紅,因為Bulider類的cookieJsr方法需要接受CookieJar類型的實例。鴨子類除了能節約代碼行數,在可理解性 可讀性 多人合作性上面都不如接口規范,誰知道鴨子類里面要寫什么方法,除了寫代碼得人自己。 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的請求參數 * @return post的返回結果 */ 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(); } }
上述內容就是java中怎么實現投放pythonsession功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。