Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the guidelines for using mica-http

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the guidelines for the use of mica-http are, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can gain something through the detailed introduction of this article.

Mica-http

Mica-http is the encapsulation of okhttp, http toolkit for Fluent syntax, and syntax reference HttpClient Fluent API.

Use maven net.dreamlu mica-http ${version} gradlecompile ("net.dreamlu:mica-http:$ {version}") to use documents

Set the global log level NONE,BASIC,HEADERS,BODY. Default: NONE

HttpRequest.setGlobalLog (LogLevel.BODY)

Synchronous request url. Methods support get, post, patch, put, and delete.

HttpRequest.get ("https://www.baidu.com") .log (LogLevel.BASIC) / / set the log level this time Takes precedence over the global .addHeader ("x-account-id", "mica001") / / adds header .addCookie (new Cookie.Builder () / / adds cookie .name ("sid") .value ("mica_user_001") .build () .query ("Q", "mica") / / sets the url parameter Url encode .queryEncoding ("name", "encodedValue") .formBuilder () / form constructor is performed by default, and the same multipartFormBuilder file upload form .add ("id", 123123) / / form parameter .execute () / initiates the request .asJsonNode () / / result set conversion. Note: if there is a network exception, an exception will be thrown directly. / / similar methods include asString, asBytes, asStream// json response: asJsonNode, asValue, asList, asMap, jackson processing / / xml, html response: asDocument,asDomValue, asDomList jsoup processing / / file file: toFile

Synchronization

String html = HttpRequest.post ("https://www.baidu.com") .execute () .onFailed ((request, e)-> consumption handling of abnormal cases such as {/ / network, but without e.printStackTrace ();}) .onResponse (ResponseSpec::asString); / / handle responses, return null directly with network exceptions, etc.

Synchronization

String text = HttpRequest.patch ("https://www.baidu.com"). Execute () .onsuccess (ResponseSpec::asString); / / onSuccess http code in [200.300) handles the response and returns null directly if there is a network exception.

Asynchronous request

HttpRequest.delete ("https://www.baidu.com") .async () / / enable async. OnFailed ((request, e)-> handling e.printStackTrace () in case of {/ / exception;}) .onResponse (responseSpec-> {/ / consumption response, note: the response stream can only read int httpCode = responseSpec.code () once) }) .onSuccessful (responseSpec-> {/ / consumption response successful http code in [200.300) / Note: the response result stream can only be read once, JsonNode jsonNode = responseSpec.asJsonNode ();}) .execute (); / / Asynchronous last launch request sample code 1Accord / sets the global log level HttpRequest.setGlobalLog (LogLevel.BODY) / directly use the jackson json path syntax private String getUserEmail (String accessToken) {return HttpRequest.get ("https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))") .addHeader (" Host "," api.linkedin.com ") .addHeader (" Connection "," Keep-Alive ") .addHeader (" Authorization ") "Bearer" + accessToken) .execute () .asJsonNode () .at ("/ elements/0/handle~0/emailAddress") .asText () } / / Asynchronous public static void test () {HttpRequest.post ("https://www.baidu.com/do-stuff") .log (LogLevel.BASIC) / / log level .formBuilder () / / form constructor .add (" a ") "b") .async () / / use async .onSuccessful (System.out::println) / / function when async succeeds. OnFailed ((request, e)-> {/ / Asynchronous failure Can not have e.printStackTrace () }) .execute ();} sample code 2HttpRequest.setGlobalLog (LogLevel.BODY) / / synchronization Return nullString html = HttpRequest.get ("www.baidu.com") .connectTimeout (Duration.ofSeconds (1000)) .query ("test", "a") .Query ("name", "three") .Query ("x", 1) .query ("abd", Base64Util.encode ("123 percent percent")) .queryEncoding ("abc", Base64Util.encode ("123 requests percent")) .execute () .onFailed (request)) E)-> {e.printStackTrace () ) .onsuccess (ResponseSpec::asString); System.out.println (html); / / synchronous call, which returns Optional, and returns Optional.empty () Optional opt = HttpRequest.post in case of exception (URI.create ("https://www.baidu.com")) .bodyString (" Important stuff ") .formBuilder () .add (" a "," b ") .execute () .onSuccessOpt (ResponseSpec::asString)) / / synchronization, consumption (processing) responseHttpRequest.post upon success ("https://www.baidu.com/some-form") .addHeader (" X-Custom-header "," stuff ") .execute () .onSuccessful (responseSpec-> {String text = responseSpec.asString (); System.out.println (text);}) / / synchronous, throw HttpRequest.get directly on exception ("https://www.baidu.com/some-form") .execute () .asString (); / / async, asynchronously execute the result, and print stack HttpRequest.get on failure (" https://www.baidu.com/some-form") .async () .onSuccessful (System.out::println) .onFailed ((request, e)-> {e.printStackTrace ()) ") }) .execute (); what are the guidelines for the use of mica-http? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report