Java's httpclient and okhttp request network, are more troublesome than the requests of PY, also more than the self-brought package of Py urllib trouble. Now think of requests is really for humans, take to use, basic no special case without two times package.
Java or to be encapsulated into a get post tool class is useful, the tool class is a static method of the class, more general, no need to new object. As with the @staticmethod method of py.
Packagecom.touna.httprequest;ImportJava.util.HashMap;ImportJava.util.Set;ImportOKHTTP3. Formbody;ImportOKHTTP3. okhttpclient;ImportOKHTTP3. Request;ImportOKHTTP3. Requestbody;ImportOKHTTP3. Response; Public classHttputil { Public Static voidPost (string URL, hashmap<string, String >Paramsmap) {//There is no return, you can return string okhttpclient mokhttpclient=Newokhttpclient (); Formbody.builder Formbodybuilder=NewFormbody.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=NewRequest. Builder (). Post (formbody). URL (URL). build (); Try(Response Response =Mokhttpclient.newcall (Request). Execute ()) {System.out.println (Response.body (). String ()); }Catch(Exception e) {e.printstacktrace (); } } Public Static voidMain (string[] args) {HashMap<String,String> Paramsmap =NewHashmap<string, string>() ; Paramsmap.put ("Name", "Xiaoming"); Paramsmap.put ("HTML", "<HTML> ..."); Post ("HTTPS://10.0.4.147:8015/CRCC", Paramsmap); }}
Java okhttp send a POST request