B2BÍøÂçÈí¼þ

 ÕÒ»ØÃÜÂë
 Á¢¼´×¢²á ÉóºËÍøÕ¾ºÅ:QQ:896757558
ËÑË÷
²é¿´: 26|»Ø¸´: 0
´òÓ¡ ÉÏÒ»Ö÷Ìâ ÏÂÒ»Ö÷Ìâ

ÔÚAIWROKÈí¼þÖÐʹÓÃOkHttp·¢ËÍGETÇëÇó²¢»ñÈ¡ºÍÊä³öHTTPÏìӦͷÐÅÏ¢

[¸´ÖÆÁ´½Ó]

787

Ö÷Ìâ

792

Ìû×Ó

6424

»ý·Ö

abc

Rank: 9Rank: 9Rank: 9

»ý·Ö
6424
Ìøתµ½Ö¸¶¨Â¥²ã
Â¥Ö÷
˵Ã÷: okhttpÊÇ°²×¿Öг£ÓõIJå¼þ,ËٶȿìÐÔÄÜÎȶ¨£¬ºÍhttpÄ£¿éÏศÏà³É£¬ÌṩÁËһЩhttpÄ£¿é²»¾ß±¸µÄ·½·¨ÀýÈçjsonÌá½»ºÍÉÏ´«ÏÂÔØÎļþµÈ

µÚÒ»¸ö£º

ÏëҪʹÓÃgetCookie()·½·¨»ñÈ¡cookie¡£¼ÙÉèÄúÏëÔÚokHttpµÄÉÏÏÂÎÄÖÐʹÓô˷½·¨£¬¿ÉÒÔ½«ÆäÖ±½ÓÌí¼Óµ½ÄúµÄ´úÂëÖС£ÒÔÏÂÊÇÒ»¸öʾÀý£¬Õ¹Ê¾ÁËÈçºÎʹÓÃgetCookie()·½·¨»ñÈ¡cookie²¢Êä³ö£º

// ´´½¨Ò»¸öеÄokHttpʵÀý
var okhttp1 = new okHttp();

// »ñÈ¡cookie
var cookie = okhttp1.getCookie();

// Êä³öcookie
printl(cookie);

Ïêϸ˵Ã÷

  1. ´´½¨okHttpʵÀý£ºÊ¹ÓÃvar¹Ø¼ü×Ö´´½¨Ò»¸öokHttpʵÀý¡£
  2. »ñÈ¡cookie£ºµ÷ÓÃgetCookie()·½·¨À´»ñÈ¡µ±Ç°µÄcookie¡£
  3. Êä³ö½á¹û£ºÊ¹ÓÃprintl·½·¨½«cookieÊä³öµ½¿ØÖÆ̨¡£

Èç¹ûÄúÐèÒªÔÚ·¢ËÍÇëÇó֮ǰÉèÖÃcookie£¬Äú¿ÉÒÔʹÓÃsetCookie·½·¨¡£ÀýÈ磺

// ´´½¨Ò»¸öеÄokHttpʵÀý
var okhttp1 = new okHttp();

// ÉèÖÃÇëÇóÍ·
okhttp1.setHeader('token', 'ÄúµÄtokenÖµ');
okhttp1.setHeader('user-agent', 'ÄúµÄUser-Agent');

// »ñÈ¡²¢ÉèÖÃcookie
var cookie = okhttp1.getCookie();
okhttp1.setCookie('cookie_name', cookie);

// ·¢ËÍÇëÇóºÍÆäËû²Ù×÷...

µÚ¶þ¸ö£º

Äú¿ÉÒÔʹÓÃsetCookie(String arg0, String arg1)·½·¨Ìí¼Ócookie¡£arg0ÊÇcookieµÄÃû³Æ£¬arg1ÊÇcookieµÄÖµ¡£

ÒÔÏÂÊÇÒ»¸öʾÀý£¬Õ¹Ê¾ÁËÈçºÎʹÓÃsetCookie·½·¨£º

// ´´½¨Ò»¸öеÄokHttpʵÀý
var okhttp1 = new okHttp();

// ÉèÖÃcookie
var cookieName = 'your_cookie_name'; // Ì滻ΪcookieÃû³Æ
var cookieValue = 'your_cookie_value'; // Ì滻ΪcookieÖµ
okhttp1.setCookie(cookieName, cookieValue);

// ·¢ËÍÇëÇó»òÆäËû²Ù×÷...
printl('CookieÒÑÉèÖÃ: ' + cookieName + '=' + cookieValue);

Ïêϸ˵Ã÷

  1. ´´½¨okHttpʵÀý£ºÊ¹ÓÃvar¹Ø¼ü×Ö´´½¨Ò»¸öokHttpʵÀý¡£
  2. ÉèÖÃcookie£ºµ÷ÓÃsetCookie·½·¨£¬´«ÈëcookieµÄÃû³ÆºÍ¶ÔÓ¦µÄÖµ¡£
  3. Êä³ö½á¹û£ºÊ¹ÓÃprintl·½·¨Êä³öÉèÖõÄcookieÐÅÏ¢¡£

Äú¿ÉÒÔÔÚ·¢ËÍÇëÇó֮ǰÉèÖÃcookie£¬ÕâÑù¿ÉÒÔÈ·±£ÇëÇóÖаüº¬ËùÐèµÄcookieÐÅÏ¢¡£Èç¹ûÄúÓÐÐèÒªÉèÖõÄʵ¼ÊcookieÃû³ÆºÍÖµ£¬ÇëÌ滻ʾÀýÖеÄռλ·û¡£

µÚÈý¸ö£º

¿ÉÒÔʹÓÃgetResponseHeaders()·½·¨»ñÈ¡HTTPÏìӦͷ¡£ÏÂÃæÊÇÒ»¸öÍêÕûµÄʾÀý£¬Õ¹Ê¾ÁËÈçºÎÔÚokHttpÖÐʹÓø÷½·¨£º

// ´´½¨Ò»¸öеÄokHttpʵÀý
var http1 = new okHttp();

// ÉèÖÃÇëÇóÍ·
http1.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Mobile Safari/537.36");

// ·¢ËÍGETÇëÇó
var res = http1.get("https://v.kuaishou.com/nQtui");

// »ñÈ¡²¢Êä³öÏìӦͷ
var responseHeaders = http1.getResponseHeaders();
printl(responseHeaders);

Ïêϸ˵Ã÷

  1. ´´½¨okHttpʵÀý£ºÊ¹ÓÃvar¹Ø¼ü×Ö´´½¨Ò»¸öokHttpʵÀý¡£
  2. ÉèÖÃÇëÇóÍ·£ºÊ¹ÓÃsetHeader·½·¨ÉèÖÃÐèÒªµÄÇëÇóÍ·£¬ÀýÈçUser-Agent¡£
  3. ·¢ËÍGETÇëÇó£ºÊ¹ÓÃget·½·¨·¢ËÍHTTP GETÇëÇóµ½Ö¸¶¨µÄURL¡£
  4. »ñÈ¡ÏìӦͷ£ºµ÷ÓÃgetResponseHeaders·½·¨»ñÈ¡ÏìӦͷ¡£
  5. Êä³öÏìӦͷ£ºÊ¹ÓÃprintl·½·¨Êä³ö»ñÈ¡µÄÏìӦͷÐÅÏ¢¡£

ͨ¹ýÕâ¸öÀý×Ó£¬Äú¿ÉÒÔ»ñÈ¡HTTPÏìÓ¦µÄËùÓÐÍ·ÐÅÏ¢²¢ÔÚ¿ØÖÆ̨Êä³ö¡£¸ù¾Ýʵ¼ÊÇé¿öµ÷ÕûÏà¹ØµÄURL»òÇëÇóÍ·¡£

µÚËĸö£º

Äú¿ÉÒÔʹÓÃsetHeader(String arg0, String arg1)·½·¨À´ÉèÖÃHTTPÇëÇóÍ·¡£arg0ÊDzÎÊýÃû³Æ£¨ÀýÈçtoken£©£¬arg1ÊDzÎÊýÖµ£¨ÀýÈç12345667£©¡£

ÒÔÏÂÊÇʹÓÃsetHeader·½·¨µÄʾÀý´úÂ룺

// ´´½¨Ò»¸öеÄokHttpʵÀý
var http1 = new okHttp();

// ÉèÖÃHTTPÍ·
http1.setHeader("token", "12345667"); // Ì滻ΪÄúµÄʵ¼Êtoken

// ·¢ËÍÇëÇó֮ǰ£¬Äú¿ÉÒÔÉèÖÃÆäËû±ØÒªµÄÇëÇóÍ·
http1.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36");

// ·¢ËÍÇëÇóÓë´¦ÀíÆäËûÂß¼­...

Ïêϸ˵Ã÷

  1. ´´½¨okHttpʵÀý£ºÊ¹ÓÃvar¹Ø¼ü×Ö´´½¨Ò»¸öokHttpʵÀý¡£
  2. ÉèÖÃHTTPÍ·£ºÊ¹ÓÃsetHeader·½·¨ÉèÖòÎÊýÃû³ÆºÍ²ÎÊýÖµ£¬ÀýÈçtokenºÍ¶ÔÓ¦µÄÖµ¡£
  3. ·¢ËÍÇëÇó£ºÉèÖÃÍêÇëÇóÍ·ºó£¬¿ÉÒÔ·¢ËÍHTTPÇëÇó²¢½øÐкóÐø´¦Àí¡£

È·±£½«Ê¾ÀýÖеIJÎÊýÃû³ÆºÍÖµÌ滻ΪÄúʵ¼ÊÐèÒªµÄÄÚÈÝ¡£Èç¹ûÐèÒªÉèÖöà¸öÇëÇóÍ·£¬¿ÉÒÔ¶à´Îµ÷ÓÃsetHeader·½·¨À´Íê³É¡£

µÚËĸö£º

Äú¿ÉÒÔʹÓÃclearHeader()·½·¨À´Çå³ýµ±Ç°µÄHTTPÇëÇóÍ·¡£ÕâÔÚÐèÒªÖØÖÃÇëÇóÍ·»òÔÚ·¢ËͶà¸öÇëÇóʱ·Ç³£ÓÐÓá£

ÒÔÏÂÊÇÈçºÎʹÓÃclearHeader()·½·¨µÄʾÀý´úÂ룺

// ´´½¨Ò»¸öеÄokHttpʵÀý
var http1 = new okHttp();

// ÉèÖÃһЩÇëÇóÍ·
http1.setHeader("token", "12345667");
http1.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36");

// ÔÚÐèÒªµÄÇé¿öÏÂÇå³ýËùÓÐÇëÇóÍ·
http1.clearHeader();

// ÏÖÔÚÇëÇóÍ·Òѱ»Çå³ý£¬¿ÉÒÔÖØÐÂÉèÖÃеÄÇëÇóÍ·
http1.setHeader("New-Header", "newValue");

// ·¢ËÍÇëÇóÓë´¦ÀíÆäËûÂß¼­...

Ïêϸ˵Ã÷

  1. ´´½¨okHttpʵÀý£ºÊ¹ÓÃvar¹Ø¼ü×Ö´´½¨Ò»¸öokHttpʵÀý¡£
  2. ÉèÖÃÇëÇóÍ·£ºÊ¹ÓÃsetHeader·½·¨ÉèÖóõʼÇëÇóÍ·¡£
  3. Çå³ýÇëÇóÍ·£ºµ÷ÓÃclearHeader()·½·¨Çå³ýËùÓе±Ç°µÄÇëÇóÍ·¡£
  4. ÖØÐÂÉèÖÃÇëÇóÍ·£ºÔÚÇå³ýºó£¬¿ÉÒÔÖØÐÂÉèÖÃеÄÇëÇóÍ·¡£
  5. ·¢ËÍÇëÇó£ºÉèÖÃÍêÇëÇóÍ·ºó£¬¿ÉÒÔ·¢ËÍHTTPÇëÇó²¢½øÐкóÐø´¦Àí¡£

ͨ¹ýÕâÖÖ·½Ê½£¬Äú¿ÉÒÔÁé»î¹ÜÀíHTTPÇëÇóÍ·£¬È·±£ËüÃÇÔÚÿ´ÎÇëÇóÖзûºÏÄúµÄÐèÇó¡£

µÚÎå¸ö£º


downloadFile·½·¨ÓÃÓÚÏÂÔØÎļþ£¬½ÓÊÜÁ½¸ö²ÎÊý£º

  1. arg0: URL£¨ÒªÏÂÔØÎļþµÄµØÖ·£©
  2. arg1: Îļþ·¾¶£¨±£´æÎļþµÄ±¾µØ·¾¶£©

ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룬չʾÈçºÎʹÓÃdownloadFile·½·¨£º

// ´´½¨Ò»¸öеÄokHttpʵÀý
var okhttp1 = new okHttp();

// ÉèÖÃÇëÇóÍ·£¨Èç¹ûÐèÒªµÄ»°£©
okhttp1.setHeader('token', 'ÄúµÄToken');

// ÒªÏÂÔصÄÎļþURL
var fileUrl = 'http://example.com/path/to/your/file.zip'; // Ì滻Ϊʵ¼ÊURL

// ±¾µØ±£´æÎļþµÄ·¾¶
var localPath = '/sdcard/Download/file.zip'; // Ì滻ΪÄúÏë±£´æµÄ·¾¶

// ÏÂÔØÎļþ
var downloadResponse = okhttp1.downloadFile(fileUrl, localPath);

// Êä³öÏÂÔؽá¹û
printl(downloadResponse);

Ïêϸ˵Ã÷

  1. ´´½¨ÊµÀý£ºÍ¨¹ýnew okHttp()´´½¨okHttpʵÀý¡£
  2. ÉèÖÃÇëÇóÍ·£º¸ù¾ÝÐèÒªÉèÖÃÇëÇóÍ·£¬Èç¹ûÐèÒªÉí·ÝÑéÖ¤µÈ¡£
  3. Ö¸¶¨ÎļþURLºÍ±£´æ·¾¶£ºÌ滻Ϊʵ¼ÊÏÂÔصÄÎļþURLºÍ±¾µØ±£´æ·¾¶¡£
  4. µ÷ÓÃdownloadFile·½·¨£ºÊ¹ÓÃdownloadFile·½·¨ÏÂÔØÎļþ£¬²¢´æ´¢·µ»Ø½á¹û¡£
  5. Êä³ö½á¹û£ºÊ¹ÓÃprintlÊä³öÏÂÔؽá¹û¡£

×¢ÒâÊÂÏî

  • È·±£ÓÐÕýÈ·µÄȨÏÞÔÚÖ¸¶¨µÄ·¾¶ÉÏдÈëÎļþ¡£
  • URL±ØÐëÓÐЧÇÒ¿ÉÒÔ·ÃÎÊ£¬ÒÔÈ·±£ÏÂÔسɹ¦¡£

Çë¸ù¾ÝÄúµÄʵ¼ÊÐèÇóÓë»·¾³µ÷Õû´úÂëƬ¶Î¡£

µÚÁù¸ö£º

downloadFileProgress·½·¨ÓÃÓÚÏÂÔØÎļþ²¢ÏÔʾÏÂÔؽø¶È¡£Ëü½ÓÊÜÒÔϲÎÊý£º

  1. arg0£ºURL£¨ÒªÏÂÔØÎļþµÄµØÖ·£©
  2. arg1£ºÎļþ·¾¶£¨±¾µØ±£´æÎļþµÄ·¾¶£©
  3. arg2£º»Øµ÷º¯Êý£¨ÓÃÓÚ¸ú×ÙÏÂÔؽø¶È£©

ʾÀý´úÂë

ÒÔÏÂÊÇÈçºÎʹÓÃdownloadFileProgress·½·¨µÄʾÀý´úÂ룺

// ÏÂÔØÎļþµÄURL
var fileUrl = "http://example.com/path/to/your/file.zip"; // Ì滻Ϊʵ¼ÊµÄÎļþURL
// ±¾µØ±£´æÎļþµÄ·¾¶
var localPath = "/sdcard/Download/file.zip"; // Ì滻ΪÄúÏë±£´æµÄ·¾¶

// ¶¨Òå»Øµ÷º¯ÊýÒÔ¸ú×ÙÏÂÔؽø¶È
function progressCallback(percentage) {
    printl("ÏÂÔؽø¶È: " + percentage + "%");
}

// µ÷ÓÃdownloadFileProgressÏÂÔØÎļþ
var response = downloadFileProgress(fileUrl, localPath, progressCallback);

// Êä³ö×îÖÕÏìÓ¦
printl(response);

´úÂë˵Ã÷

  1. ÎļþURLºÍ±£´æ·¾¶£º½«ÎļþURLºÍÏÂÔر£´æµÄ·¾¶Ì滻ΪÄúÐèÒªµÄÄÚÈÝ¡£
  2. »Øµ÷º¯Êý£ºprogressCallbackº¯Êý½«½ÓÊÕÏÂÔصĽø¶È°Ù·Ö±È£¬²¢Í¨¹ýprintlÊä³ö¡£
  3. µ÷Ó÷½·¨£ºÊ¹ÓÃdownloadFileProgress·½·¨½øÐÐÎļþÏÂÔØ£¬²¢´«ÈëÏàÓ¦µÄ²ÎÊý¡£

×¢ÒâÊÂÏî

  • È·±£ÍøÂçÁ¬½ÓÕý³£ÇÒÄ¿±êÎļþURLÓÐЧ¡£
  • È·±£Ó¦ÓóÌÐò¾ßÓзÃÎʴ洢ȨÏÞ£¬ÒԱ㱣´æÏÂÔØÎļþ¡£

µÚÁù¸ö£º

get·½·¨ÓÃÓÚ·¢ËÍGETÇëÇ󣬽ÓÊÜÒ»¸öURL×÷Ϊ²ÎÊý£¬²¢·µ»ØÒ»¸ö×Ö·û´®ÏìÓ¦¡£

ʾÀý´úÂë

ÒÔÏÂÊÇÈçºÎʹÓÃget·½·¨µÄʾÀý´úÂ룺

// ¶¨ÒåÇëÇóµÄURL
var url = "http://kg.lbzyw.top/api/banks/collect"; // Ì滻Ϊʵ¼ÊµÄURL

// ·¢ËÍGETÇëÇó
var response = http.get(url);

// Êä³öÏìÓ¦µÄ½á¹û
printl(response);

´úÂë˵Ã÷

  1. URLÉèÖ㺽«ÇëÇóµÄURLÌ滻ΪÄúµÄʵ¼ÊÄ¿±êµØÖ·¡£
  2. ·¢ËÍÇëÇó£ºÊ¹ÓÃhttp.get(url)·¢ËÍÇëÇó¡£
  3. Êä³öÏìÓ¦£º½«ÏìÓ¦½á¹ûͨ¹ýprintlÊä³ö¡£

×¢ÒâÊÂÏî

  • È·±£URLÓÐЧ²¢ÇÒ¿ÉÒÔ·ÃÎÊ¡£
  • ¼ì²éÍøÂçÁ¬½ÓÇé¿ö£¬ÒÔÈ·±£Äܹ»³É¹¦·¢ËÍÇëÇó¡£

µÚÆ߸ö£º

getCookie·½·¨ÓÃÓÚ»ñÈ¡µ±Ç°µÄCookie£¬²¢·µ»ØÒ»¸ö×Ö·û´®¡£

ʾÀý´úÂë

ÒÔÏÂÊÇÈçºÎʹÓÃgetCookie·½·¨µÄʾÀý´úÂ룺

// »ñÈ¡µ±Ç°Cookie
var cookie = getCookie();

// Êä³öCookieµÄÖµ
printl(cookie);

´úÂë˵Ã÷

  1. µ÷ÓÃgetCookie·½·¨£ºÖ±½Óµ÷Óø÷½·¨ÒÔ»ñÈ¡µ±Ç°µÄCookie×Ö·û´®¡£
  2. Êä³öCookieÖµ£ºÊ¹ÓÃprintlº¯ÊýÊä³ö»ñµÃµÄCookieÖµ¡£

×¢ÒâÊÂÏî

  • È·±£ÔÚºÏÊʵÄÉÏÏÂÎÄÖе÷Óô˷½·¨£¬Í¨³£ÊÇÔÚÒѽ¨Á¢µÄ»á»°ºó¡£
  • Êä³öµÄCookie¿ÉÄÜ°üº¬¶à¸ö¼üÖµ¶Ô£¬Çë¸ù¾ÝÐèÒª½øÐнâÎö¡£
    µÚ°Ë¸ö£º

getResponseHeaders·½·¨ÓÃÓÚ»ñÈ¡HTTPÏìӦͷ£¬²¢·µ»ØÒ»¸ö×Ö·û´®¡£ÒÔÏÂÊÇÈçºÎʹÓÃÕâ¸ö·½·¨µÄʾÀý´úÂë¡£

ʾÀý´úÂë

// ´´½¨Ò»¸öokHttpʵÀý
var http1 = new okHttp();

// ÉèÖÃÇëÇóÍ·
http1.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Mobile Safari/537.36");

// ·¢ËÍGETÇëÇó
var res = http1.get("https://v.kuaishou.com/nQtuiI");

// »ñÈ¡²¢´òÓ¡·µ»ØµÄHTTPÍ·
var headers = http1.getResponseHeaders();
printl(headers);

´úÂë˵Ã÷

  1. ´´½¨okHttpʵÀý£ºÍ¨¹ýnew okHttp()À´´´½¨Ò»¸öHTTPʵÀý¡£
  2. ÉèÖÃÇëÇóÍ·£ºÊ¹ÓÃsetHeader·½·¨ÉèÖÃUser-Agent¡£
  3. ·¢ËÍGETÇëÇ󣺵÷ÓÃhttp1.get()·¢ËÍÇëÇó¡£
  4. »ñÈ¡ÏìӦͷ£ºµ÷ÓÃhttp1.getResponseHeaders()»ñÈ¡HTTPÏìӦͷ£¬²¢½«ÆäÊä³ö¡£

×¢ÒâÊÂÏî

  • È·±£URLÊÇÓÐЧµÄ£¬²¢ÇÒ¿ÉÒÔ´ÓÄúµÄÉ豸·ÃÎÊ¡£
  • Êä³öµÄÏìӦͷ¿ÉÄÜ»á°üº¬¶à¸ö¼üÖµ¶Ô£¬Äú¿ÉÒÔ¸ù¾ÝÐèÒª½øÒ»²½´¦Àí»ò½âÎö¡£

µÚ¾Å¸ö£º

json·½·¨ÓÃÓÚ·¢ËÍJSON¸ñʽµÄÊý¾Ý¡£Ëü½ÓÊÜÁ½¸ö²ÎÊý£ºÒ»¸öÊÇURL£¨arg0£©£¬Ò»¸öÊÇJSON¶ÔÏó£¨arg1£©£¬È»ºó·µ»ØÒ»¸ö×Ö·û´®¡£

ʾÀý´úÂë

ÒÔÏÂÊÇÈçºÎʹÓÃjson·½·¨µÄʾÀý´úÂ룺

// ´´½¨Ò»¸öokHttpʵÀý
var http1 = new okHttp();

// ×¼±¸JSONÊý¾Ý
var jsonData = {
    'a': '1',
    'b': '2'
};

// ·¢ËÍJSONÇëÇó
var response = http1.json('http://www.baidu.com', jsonData);

// Êä³ö·µ»Ø½á¹û
printl(response);

´úÂë˵Ã÷

  1. ´´½¨okHttpʵÀý£ºÊ¹Óànew okHttp() ´´½¨Ò»¸öеÄHTTPʵÀý¡£
  2. ×¼±¸JSONÊý¾Ý£º¶¨ÒåÒ»¸öJavaScript¶ÔÏ󣬲¢½«Æä×÷ΪJSONÊý¾Ý·¢ËÍ¡£
  3. ·¢ËÍÇëÇ󣺵÷ÓÃhttp1.json(url, jsonData)·¢ËÍHTTPÇëÇó£¬URLºÍÒª·¢Ë͵ÄJSONÊý¾Ý¡£
  4. ´òÓ¡ÏìÓ¦£º½«ÏìÓ¦½á¹ûͨ¹ýprintlÊä³ö¡£

×¢ÒâÊÂÏî

  • È·±£URLÓÐЧÇÒÄܹ»½ÓÊÜJSONÊý¾Ý¡£
  • ÑéÖ¤JSONÊý¾Ý¸ñʽÊÇ·ñÕýÈ·£¬È·±£¿ÉÒÔ±»·þÎñÆ÷ÕýÈ·½âÎö¡£

µÚÊ®¸ö£º

POST ÇëÇóµÄº¯Êý˵Ã÷£¬°üº¬·½·¨µÄ²ÎÊýºÍʾÀý´úÂë¡£ÒÔÏÂÊÇÏêϸµÄ½éÉÜ£º

º¯Êý˵Ã÷

º¯ÊýÃû³Æpost

·µ»ØÖµString

²ÎÊý:

  • String arg0: ÕâÊÇ POST ÇëÇóµÄ URL¡£
  • map arg1: ÕâÊÇÒ»¸ö°üº¬Òª·¢Ë굀 POST Êý¾ÝµÄ¼üÖµ¶Ô¼¯ºÏ¡£

ʾÀý´úÂë

¼ÙÉèÎÒÃÇҪʹÓôˠpost ·½·¨·¢ËÍ POST ÇëÇó£¬ÒÔÏÂÊÇ´úÂëʾÀý£º

// ´´½¨Ò»¸ö OkHttp ʵÀý
var http = new OkHttp();

// ×¼±¸ POST Êý¾Ý£¬Ê¹Óüòµ¥µÄ¶ÔÏó
var postData = {
    "name": "ÕÅÈý",
    "age": "25"
};

// ·¢ËÍ POST ÇëÇó
var url = "http://example.com/api"; // Ì滻Ϊʵ¼ÊµÄ URL
var response = http.post(url, postData);

// Êä³ö·þÎñÆ÷ÏìÓ¦
printl(response);

´úÂë˵Ã÷

  1. ´´½¨ OkHttp ʵÀý£ºÊ¹Óànew OkHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£
  2. ×¼±¸ POST Êý¾Ý£º¹¹ÔìÒ»¸ö¼òµ¥µÄ¶ÔÏóÀ´´æ´¢Òª·¢Ë͵ÄÊý¾Ý¡£
  3. ·¢ËÍÇëÇ󣺵÷Óàhttp.post(url, postData) ·½·¨·¢ËÍ POST ÇëÇ󣬲¢´«ÈëÄ¿±ê URL ºÍ POST Êý¾Ý¡£
  4. Êä³öÏìÓ¦£ºÊ¹Óàprintl() º¯Êý½«·þÎñÆ÷µÄÏìÓ¦½á¹û´òÓ¡³öÀ´¡£

×¢ÒâÊÂÏî

  • È·±£ URL ÓÐЧÐÔ£ºÇëÈ·±£ÄúÌṩµÄ URL ÊÇÓÐЧÇÒ¿É·ÃÎʵġ£
  • Êý¾Ý¸ñʽ£ºÈ·ÈÏ POST Êý¾ÝµÄ¸ñʽ·ûºÏ·þÎñÆ÷µÄÒªÇó£¬ÒÔ±ãÄÜÕýÈ·½ÓÊÕ¡£

ͨ¹ýÒÔÉϲ½Ö裬Äú¿ÉÒÔʹÓàpost ·½·¨·¢ËÍ POST ÇëÇó²¢´¦Àí·þÎñÆ÷µÄÏìÓ¦¡£Èç¹ûÓÐÌض¨ÐèÇ󣬿ÉÒÔ¸ù¾Ýʵ¼ÊÇé¿ö½øÐе÷Õû¡£

µÚʮһ¸ö£º

·¢ËÍ POST ±íµ¥ÇëÇóµÄº¯Êý˵Ã÷£¬°üÀ¨·½·¨µÄ²ÎÊýºÍʾÀý´úÂë¡£ÒÔÏÂÊÇÏêϸµÄ½éÉÜ£º

º¯Êý˵Ã÷

º¯ÊýÃû³ÆpostForm

·µ»ØÖµString

²ÎÊý:

  • String arg0: ÕâÊÇ POST ÇëÇóµÄ URL¡£
  • String arg1: ÕâÊÇÒª·¢Ë굀 POST Êý¾Ý£¨Í¨³£ÒÔ²éѯ×Ö·û´®¸ñʽ´«µÝ£©¡£

ʾÀý´úÂë

ÒÔÏÂÊÇÈçºÎʹÓàpostForm ·½·¨µÄʾÀý´úÂ룺

// ´´½¨Ò»¸ö OkHttp ʵÀý
var http = new OkHttp();

// ×¼±¸ POST Êý¾Ý£¬ÒÔ²éѯ×Ö·û´®¸ñʽ
var postData = "name=ÕÅÈý&age=25";

// ·¢ËÍ POST ÇëÇó
var url = "http://example.com/api"; // Ì滻Ϊʵ¼ÊµÄ URL
var response = http.postForm(url, postData);

// Êä³ö·þÎñÆ÷ÏìÓ¦
printl(response);

´úÂë˵Ã÷

  1. ´´½¨ OkHttp ʵÀý£ºÊ¹Óànew OkHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£
  2. ×¼±¸ POST Êý¾Ý£ºÒÔ²éѯ×Ö·û´®µÄ¸ñʽ׼±¸Òª·¢Ë͵ÄÊý¾Ý£¬Èç name=ÕÅÈý&age=25¡£
  3. ·¢ËÍÇëÇ󣺵÷Óàhttp.postForm(url, postData) ·½·¨·¢ËÍ POST ÇëÇ󣬲¢´«ÈëÄ¿±ê URL ºÍ POST Êý¾Ý¡£
  4. Êä³öÏìÓ¦£ºÊ¹Óàprintl() ½«·þÎñÆ÷µÄÏìÓ¦½á¹û´òÓ¡³öÀ´¡£

×¢ÒâÊÂÏî

  • È·±£ URL ÓÐЧÐÔ£ºÇëÈ·ÈÏÄúÌṩµÄ URL ÊÇÓÐЧÇÒ¿É·ÃÎʵġ£
  • Êý¾Ý¸ñʽ£ºÈ·±£ POST Êý¾ÝµÄ¸ñʽ·ûºÏ·þÎñÆ÷µÄÒªÇó£¬Í¨³£Îª URL ±àÂë¸ñʽ¡£

µÚÊ®¶þ¸ö£º

ÉèÖÃ×Ö·û¼¯µÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º

º¯Êý˵Ã÷

º¯ÊýÃû³ÆsetCharSet

·µ»ØÖµvoid

²ÎÊý:

  • String arg0: ÕâÊÇÒªÉèÖõÄ×Ö·û¼¯£¬¿ÉÒÔÊÇ UTF-8 »ò GBK¡£´«Èë null ½«²»½øÐбàÂ룬ĬÈÏΪ UTF-8¡£

ʾÀý´úÂë

ÏÂÃæÊÇÈçºÎʹÓàsetCharSet ·½·¨½øÐÐ×Ö·û¼¯ÉèÖõÄʾÀý´úÂ룺

// ´´½¨Ò»¸ö OkHttp ʵÀý
var http = new OkHttp();

// ÉèÖÃ×Ö·û¼¯Îª UTF-8
http.setCharSet("UTF-8");

// »òÕßÉèÖÃΪ GBK
http.setCharSet("GBK");

// Èç¹û²»Ïë½øÐбàÂ룬¿ÉÒÔÉèÖÃΪ null
http.setCharSet(null);

´úÂë˵Ã÷

  1. ´´½¨ OkHttp ʵÀý: ʹÓànew OkHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£
  2. ÉèÖÃ×Ö·û¼¯:
    • ʹÓàhttp.setCharSet("UTF-8") ÉèÖÃ×Ö·û¼¯Îª UTF-8¡£
    • ʹÓàhttp.setCharSet("GBK") ÉèÖÃ×Ö·û¼¯Îª GBK¡£
    • ʹÓàhttp.setCharSet(null) Èç¹û²»Ïë½øÐÐÌØÊâ±àÂë¡£

×¢ÒâÊÂÏî

  • ºÏÀíÑ¡Ôñ×Ö·û¼¯: ¸ù¾Ýʵ¼ÊÐèÇóÑ¡ÔñºÏÊʵÄ×Ö·û¼¯À´È·±£Êý¾ÝÕýÈ·´«ÊäºÍ´æ´¢¡£
  • ĬÈÏÖµ: Èç¹ûδÉèÖÃ×Ö·û¼¯£¬Ä¬ÈÏΪ UTF-8¡£

ͨ¹ýÒÔÉϲ½Ö裬Äú¿ÉÒÔʹÓàsetCharSet ·½·¨Áé»îÉèÖÃ×Ö·û¼¯£¬ÒÔÂú×㲻ͬµÄ±àÂëÐèÇ󡣸ù¾ÝÄúµÄ¾ßÌ峡¾°½øÐе÷Õû¡£

µÚÊ®Èý¸ö£º

Ìí¼Ó Cookie µÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º

º¯Êý˵Ã÷

º¯ÊýÃû³ÆsetCookie

·µ»ØÖµvoid

²ÎÊý:

  • String arg0: ²ÎÊýÃû³Æ£¨Cookie µÄÃû³Æ£©¡£
  • String arg1: ²ÎÊýÖµ£¨Cookie µÄÖµ£©¡£

ʾÀý´úÂë

ÏÂÃæÊÇÈçºÎʹÓàsetCookie ·½·¨Ìí¼Ó Cookie µÄʾÀý´úÂ룺

// ´´½¨Ò»¸ö OkHttp ʵÀý
var http = new OkHttp();

// Ìí¼Ó Cookie
var cookieName = "session_id"; // Cookie Ãû³Æ
var cookieValue = "abc123xyz";  // Cookie Öµ

http.setCookie(cookieName, cookieValue);

// ¿ÉÑ¡£º´òÓ¡È·ÈÏÐÅÏ¢
printl("Cookie ÒÑÉèÖÃ: " + cookieName + "=" + cookieValue);

´úÂë˵Ã÷

  1. ´´½¨ OkHttp ʵÀý: ʹÓànew OkHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£
  2. ÉèÖÃ Cookie:
    • ͨ¹ýµ÷Óàhttp.setCookie(cookieName, cookieValue) ½«Ö¸¶¨µÄ Cookie Ãû³ÆºÍÖµÌí¼Óµ½ÇëÇóÖС£

×¢ÒâÊÂÏî

  • È·±£ÓÐЧÐÔ: È·±£ÉèÖÃµÄ Cookie Ãû³ÆºÍÖµ·ûºÏ·þÎñÆ÷µÄÔ¤ÆÚ¡£
  • Cookie ×÷ÓÃ: Ìí¼ÓµÄ Cookie ½«»áÔÚºóÐøµÄÇëÇóÖз¢Ë͸ø·þÎñÆ÷£¬ÒÔ±ãÓڻỰ¹ÜÀí»òÓû§Éí·ÝÑéÖ¤¡£

µÚÊ®Ëĸö£º

¹ØÓÚÉèÖà HTTP Í·µÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º

º¯Êý˵Ã÷

º¯ÊýÃû³ÆsetHeader

·µ»ØÖµvoid

²ÎÊý:

  • String arg0: ²ÎÊýÃû³Æ£¨¼´ HTTP Í·µÄÃû³Æ£©¡£
  • String arg1: ²ÎÊýÖµ£¨¼´ HTTP Í·¶ÔÓ¦µÄÖµ£©¡£

ʾÀý´úÂë

ÒÔÏÂÊÇÈçºÎʹÓàsetHeader ·½·¨ÉèÖà HTTP Í·µÄʾÀý´úÂ룺

// ´´½¨Ò»¸ö OkHttp ʵÀý
let http1 = new okHttp();

// ÉèÖÃ HTTP Í·
http1.setHeader("token", "12345667");

// ¿ÉÑ¡£ºÉèÖÃÆäËû HTTP Í·
http1.setHeader("User-Agent", "Mozilla/5.0");

// ºóÐøÇëÇ󽫻áʹÓÃÕâЩÉèÖõÄÍ·ÐÅÏ¢

´úÂë˵Ã÷

  1. ´´½¨ OkHttp ʵÀý: ʹÓànew okHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£
  2. ÉèÖÃ HTTP Í·:
    • ͨ¹ýµ÷Óàhttp1.setHeader("token", "12345667") ÉèÖÃÃûΪ token µÄ HTTP Í·¡£
    • ͬÑù¿ÉÒÔÉèÖÃÆäËû HTTP Í·£¬±ÈÈç User-Agent¡£

×¢ÒâÊÂÏî

  • È·±£ÕýÈ·ÐÔ: È·±£ÉèÖõÄÍ·ÐÅÏ¢·ûºÏ·þÎñÆ÷µÄÒªÇ󣬱ÜÃâÒò´íÎóµÄÍ·ÐÅÏ¢µ¼ÖÂÇëÇóʧ°Ü¡£
  • ¶à¸öÍ·ÐÅÏ¢: ¿ÉÒÔ¶à´Îµ÷ÓàsetHeader ·½·¨ÉèÖöà¸ö HTTP Í·¡£

µÚÊ®Îå¸ö£º

¹ØÓÚÉèÖÃÊý¾Ý·¢Ëͳ¬Ê±Ê±¼äµÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º

º¯Êý˵Ã÷

º¯ÊýÃû³ÆsetTimeOut

·µ»ØÖµvoid

²ÎÊý:

  • int arg0: ·¢Ëͳ¬Ê±Ê±¼ä£¨µ¥Î»£ºÃ룩¡£
  • int arg1: ¶ÁÈ¡³¬Ê±Ê±¼ä£¨µ¥Î»£ºÃ룩¡£
  • int arg2: Á¬½Ó³¬Ê±Ê±¼ä£¨µ¥Î»£ºÃ룩¡£

ʾÀý´úÂë

ÒÔÏÂÊÇÈçºÎʹÓàsetTimeOut ·½·¨ÉèÖó¬Ê±Ê±¼äµÄʾÀý´úÂ룺

// ´´½¨Ò»¸ö OkHttp ʵÀý
let http1 = new okHttp();

// ÉèÖó¬Ê±Ê±¼ä
http1.setTimeOut(30, 15, 10); // ·¢Ëͳ¬Ê± 30 Ã룬¶ÁÈ¡³¬Ê± 15 Ã룬Á¬½Ó³¬Ê± 10 Ãë

´úÂë˵Ã÷

  1. ´´½¨ OkHttp ʵÀý: ʹÓànew okHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£
  2. ÉèÖó¬Ê±Ê±¼ä:
    • µ÷Óàhttp1.setTimeOut(30, 15, 10) ÉèÖ÷¢Ëͳ¬Ê±Ê±¼äΪ 30 Ã룬¶ÁÈ¡³¬Ê±Ê±¼äΪ 15 Ã룬Á¬½Ó³¬Ê±Ê±¼äΪ 10 Ãë¡£

×¢ÒâÊÂÏî

  • ºÏÀíÉèÖó¬Ê±Ê±¼ä: ¸ù¾ÝÍøÂç»·¾³ºÍÇëÇóµÄÐèÇ󣬺ÏÀíÉèÖó¬Ê±Ê±¼ä£¬ÒÔ·ÀÖ¹ÇëÇóÒò³¬Ê±¶øʧ°Ü¡£
  • µ¥Î»: ËùÓг¬Ê±Ê±¼äµÄµ¥Î»¾ùΪÃ룬ϵͳ»á×Ô¶¯×ª»»ÎªÊʵ±µÄºÁÃëÖµ½øÐд¦Àí¡£

µÚÊ®Áù¸ö£º

¹ØÓÚÎļþÉÏ´«µÄº¯Êý˵Ã÷¡£ÒÔÏÂÊÇÏêϸ½éÉÜ£º

º¯Êý˵Ã÷

º¯ÊýÃû³ÆuploadFile

·µ»ØÖµString

²ÎÊý:

  • String arg0: Ä¿±ê URL£¨¼´ÎļþÉÏ´«µÄµØÖ·£©¡£
  • String arg1: ²ÎÊýÃû³Æ£¨ÓÃÓÚÉÏ´«µÄ²ÎÊýÃû£¬Í¨³£ÊÇÎļþ×ֶεÄÃû³Æ£©¡£
  • String arg2: Îļþ·¾¶£¨¼´ÒªÉÏ´«µÄÎļþÔÚ±¾µØµÄ·¾¶£©¡£
  • map arg3: ÆäËû±íµ¥Êý¾Ý£¨ÒÔ¼üÖµ¶ÔÐÎʽ´«µÝµÄ¶îÍâÊý¾Ý£©¡£

ʾÀý´úÂë

ÒÔÏÂÊÇÈçºÎʹÓàuploadFile ·½·¨ÉÏ´«ÎļþµÄʾÀý´úÂ룺

// ´´½¨Ò»¸ö OkHttp ʵÀý
let http = new okHttp();

// ×¼±¸Îļþ·¾¶ºÍ²ÎÊý
let url = "http://example.com/upload"; // Ì滻Ϊʵ¼ÊµÄ URL
let paramName = "file"; // ÉÏ´«ÎļþµÄ²ÎÊýÃû³Æ
let filePath = "/path/to/your/file.txt"; // Ì滻Ϊʵ¼ÊµÄÎļþ·¾¶

// ×¼±¸ÆäËû±íµ¥Êý¾Ý
let additionalData = {
    "description": "This is a test file.",
    "userId": "12345"
};

// Ö´ÐÐÎļþÉÏ´«
let response = http.uploadFile(url, paramName, filePath, additionalData);

// Êä³öÏìÓ¦½á¹û
printl(response);

´úÂë˵Ã÷

  1. ´´½¨ OkHttp ʵÀý: ʹÓànew okHttp() ´´½¨Ò»¸öÐ嵀 HTTP ÇëÇóʵÀý¡£
  2. ×¼±¸ÉÏ´«ÎļþµÄ²ÎÊý:
    • Ö¸¶¨ÎļþÉÏ´«µÄ URL¡¢²ÎÊýÃû³ÆÒÔ¼°ÎļþµÄ±¾µØ·¾¶¡£
  3. ×¼±¸ÆäËû±íµ¥Êý¾Ý: ´´½¨Ò»¸ö¶ÔÏóÀ´´æ´¢¸½¼ÓµÄ±íµ¥Êý¾Ý¡£
  4. Ö´ÐÐÎļþÉÏ´«: µ÷ÓàuploadFile ·½·¨½øÐÐÎļþÉÏ´«£¬²¢´«ÈëÏàÓ¦µÄ²ÎÊý¡£
  5. Êä³öÏìÓ¦: ʹÓàprintl() ´òÓ¡·þÎñÆ÷µÄÏìÓ¦½á¹û¡£

×¢ÒâÊÂÏî

  • È·±£ URL ÓÐЧÐÔ: ÇëÈ·ÈÏËùÌṩµÄ URL ÊÇÓÐЧÇÒ¿É·ÃÎʵġ£
  • Îļþ·¾¶: È·±£ËùÖ¸¶¨µÄÎļþ·¾¶ÊÇÕýÈ·µÄ£¬²¢ÇÒÓжÁȡȨÏÞ¡£
  • Êý¾Ý¸ñʽ: ÉÏ´«µÄÎļþºÍÆäËû±íµ¥Êý¾ÝÓ¦·ûºÏ·þÎñÆ÷µÄÒªÇó£¬ÒÔ±ÜÃâÉÏ´«Ê§°Ü¡£

ͨ¹ýÒÔÉϲ½Ö裬Äú¿ÉÒÔʹÓàuploadFile ·½·¨ÇáËÉÉÏ´«Îļþ²¢´¦Àí·þÎñÆ÷µÄÏìÓ¦¡£ÈçÐè¸ù¾Ýʵ¼ÊÇé¿ö½øÐе÷Õû£¬È·±£´úÂëÂß¼­·ûºÏÐèÇó¡£

//1. »ñÈ¡Cookie
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý
var okhttp1 = new okHttp();
// »ñÈ¡Cookie£¬¼ÙÉègetCookie·½·¨ÓÃÓÚ»ñÈ¡µ±Ç°»á»°µÄCookie
var cookie = okhttp1.getCookie();
// ´òÓ¡»ñÈ¡µ½µÄCookie
printl(cookie);

//2. ÉèÖÃCookie
// Ôٴδ´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý
var okhttp1 = new okHttp();
// ÉèÖÃHTTPÇëÇóÍ·ÖеÄ'token'×ֶΣ¬ÖµÎª'ÄúµÄtokenÖµ'
okhttp1.setHeader('token', 'ÄúµÄtokenÖµ');
// ÉèÖÃHTTPÇëÇóÍ·ÖеÄ'user-agent'×ֶΣ¬ÖµÎª'ÄúµÄUser-Agent'
okhttp1.setHeader('user-agent', 'ÄúµÄUser-Agent');
// »ñÈ¡µ±Ç°»á»°µÄCookie£¬¼ÙÉègetCookie·½·¨ÓÃÓÚ»ñÈ¡µ±Ç°»á»°µÄCookie
var cookie = okhttp1.getCookie();
// ÉèÖÃCookie£¬¼ÙÉèsetCookie·½·¨ÓÃÓÚÉèÖÃÖ¸¶¨Ãû³ÆºÍÖµµÄCookie
okhttp1.setCookie('cookie_name', cookie);
// ´òÓ¡ÉèÖõÄCookieÐÅÏ¢£¬×¢ÒâÕâÀïµÄ±äÁ¿ÃûÓ¦¸ÃÊÇcookie¶ø²»ÊÇcookieName£¬²¢ÇÒ±äÁ¿ÖµÓ¦¸ÃÊÇcookie¶ø²»ÊÇcookieValue
printl('CookieÒÑÉèÖÃ: cookie_name=' + cookie);

//3. »ñÈ¡HTTPÏìӦͷ
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý
var http1 = new okHttp();
// ÉèÖÃHTTPÇëÇóÍ·ÖеÄ"user-agent"×ֶΣ¬ÖµÎª"Mozilla/5.0"
http1.setHeader("User-Agent", "Mozilla/5.0");
// ·¢ËÍGETÇëÇóµ½Ö¸¶¨µÄURL£¬²¢»ñÈ¡ÏìÓ¦½á¹û
var res = http1.get("https://v.kuaishou.com/nQtui");
// »ñÈ¡HTTPÏìӦͷ£¬¼ÙÉègetResponseHeaders·½·¨ÓÃÓÚ»ñÈ¡ÏìӦͷÐÅÏ¢
var responseHeaders = http1.getResponseHeaders();
// ´òÓ¡HTTPÏìӦͷÐÅÏ¢
printl(responseHeaders);

//4. ÉèÖÃHTTPÇëÇóÍ·
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý²¢ÃüÃûΪhttp1
let http1 = new okHttp();
// ÉèÖÃHTTPÇëÇóÍ·ÖеÄ"token"×ֶΣ¬ÖµÎª"12345667"
http1.setHeader("token", "12345667");
// ÉèÖÃHTTPÇëÇóÍ·ÖеÄ"user-agent"×ֶΣ¬ÖµÎª"Mozilla/5.0"
http1.setHeader("User-Agent", "Mozilla/5.0");

//5. Çå³ýHTTPÇëÇóÍ·
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý²¢ÃüÃûΪhttp1
var http1 = new okHttp();
// ÉèÖÃHTTPÇëÇóÍ·ÖеÄ"token"×ֶΣ¬ÖµÎª"12345667"
http1.setHeader("token", "12345667");
// ÉèÖÃHTTPÇëÇóÍ·ÖеÄ"user-agent"×ֶΣ¬ÖµÎª"Mozilla/5.0"
http1.setHeader("User-Agent", "Mozilla/5.0");
// Çå³ýËùÓÐHTTPÇëÇóÍ·£¬¼ÙÉèclearHeader·½·¨ÓÃÓÚÇå³ýËùÓÐÇëÇóÍ·
http1.clearHeader();
// ÉèÖÃÒ»¸öеÄHTTPÇëÇóÍ·£¬Ãû³ÆΪ"New-Header"£¬ÖµÎª"newValue"
http1.setHeader("New-Header", "newValue");

//6. ÏÂÔØÎļþ
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý
var okhttp1 = new okHttp();
// ÉèÖÃHTTPÇëÇóÍ·ÖеÄ"token"×ֶΣ¬ÖµÎª"ÄúµÄToken"
okhttp1.setHeader('token', 'ÄúµÄToken');
// ¶¨ÒåÒªÏÂÔصÄÎļþµÄURL
var fileUrl = 'http://example.com/path/to/your/file.zip';
// ¶¨ÒåÏÂÔØÎļþºó´æ´¢µÄ±¾µØ·¾¶
var localPath = '/sdcard/Download/file.zip';
// µ÷ÓÃdownloadFile·½·¨ÏÂÔØÎļþ£¬²¢´«ÈëÎļþURLºÍ±¾µØ´æ´¢Â·¾¶£¬¼ÙÉè¸Ã·½·¨ÓÃÓÚÏÂÔØÎļþ
var downloadResponse = okhttp1.downloadFile(fileUrl, localPath);
// ´òÓ¡ÏÂÔØÏìÓ¦½á¹û
printl(downloadResponse);

//7. ÏÂÔØÎļþ²¢ÏÔʾ½ø¶È
// ¶¨ÒåÒªÏÂÔصÄÎļþµÄURL
var fileUrl = "http://example.com/path/to/your/file.zip";
// ¶¨ÒåÏÂÔØÎļþºó´æ´¢µÄ±¾µØ·¾¶
var localPath = "/sdcard/Download/file.zip";
// ¶¨Òå½ø¶È»Øµ÷º¯Êý£¬ÓÃÓÚÔÚÏÂÔعý³ÌÖÐÏÔʾ½ø¶È°Ù·Ö±È
function progressCallback(percentage) {
    printl("ÏÂÔؽø¶È: " + percentage + "%");
}
// µ÷ÓÃdownloadFileProgress·½·¨ÏÂÔØÎļþ£¬²¢´«ÈëÎļþURL¡¢±¾µØ´æ´¢Â·¾¶ºÍ½ø¶È»Øµ÷º¯Êý£¬¼ÙÉè¸Ã·½·¨ÓÃÓÚÏÂÔØÎļþ²¢ÏÔʾ½ø¶È
var response = downloadFileProgress(fileUrl, localPath, progressCallback);
// ´òÓ¡ÏÂÔØÏìÓ¦½á¹û
printl(response);

//8. ·¢ËÍGETÇëÇó
// ¶¨ÒåÒªÇëÇóµÄURL
var url = "http://kg.lbzyw.top/api/banks/collect";
// ·¢ËÍGETÇëÇóµ½Ö¸¶¨µÄURL£¬²¢»ñÈ¡ÏìÓ¦½á¹û
var response = http.get(url);
// ´òÓ¡GETÇëÇóµÄÏìÓ¦½á¹û
printl(response);

//9. ·¢ËÍJSON¸ñʽµÄÊý¾Ý
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý
var http1 = new okHttp();
// ¶¨ÒåÒª·¢Ë͵ÄJSON¸ñʽµÄÊý¾Ý
var jsonData = {
    'a': '1',
    'b': '2'
};
// ·¢ËÍJSON¸ñʽµÄÊý¾Ýµ½Ö¸¶¨µÄURL£¬²¢»ñÈ¡ÏìÓ¦½á¹û£¬¼ÙÉèjson·½·¨ÓÃÓÚ·¢ËÍJSONÊý¾Ý
var response = http1.json('http://www.baidu.com', jsonData);
// ´òÓ¡·¢ËÍJSON¸ñʽÊý¾ÝºóµÄÏìÓ¦½á¹û
printl(response);

//10. ·¢ËÍPOSTÇëÇó
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý
var http = new OkHttp();
// ¶¨ÒåÒª·¢Ë͵ÄPOST¸ñʽµÄÊý¾Ý
var postData = {
    "name": "ÕÅÈý",
    "age": "25"
};
// ¶¨ÒåÒªÇëÇóµÄURL
var url = "http://example.com/api";
// ·¢ËÍPOST¸ñʽµÄÊý¾Ýµ½Ö¸¶¨µÄURL£¬²¢»ñÈ¡ÏìÓ¦½á¹û
var response = http.post(url, postData);
// ´òÓ¡POSTÇëÇóµÄÏìÓ¦½á¹û
printl(response);

//11. ·¢ËÍPOST±íµ¥ÇëÇó
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý
var http = new OkHttp();
// ¶¨ÒåÒª·¢Ë͵ÄPOST±íµ¥¸ñʽµÄÊý¾Ý
var postData = "name=ÕÅÈý&age=25";
// ¶¨ÒåÒªÇëÇóµÄURL
var url = "http://example.com/api";
// ·¢ËÍPOST±íµ¥¸ñʽµÄÊý¾Ýµ½Ö¸¶¨µÄURL£¬²¢»ñÈ¡ÏìÓ¦½á¹û£¬¼ÙÉèpostForm·½·¨ÓÃÓÚ·¢ËÍPOST±íµ¥Êý¾Ý
var response = http.postForm(url, postData);
// ´òÓ¡POST±íµ¥ÇëÇóµÄÏìÓ¦½á¹û
printl(response);

//12. ÉèÖÃ×Ö·û¼¯
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý
var http = new OkHttp();
// ÉèÖÃ×Ö·û¼¯ÎªUTF-8
http.setCharSet("UTF-8");
// ÉèÖÃ×Ö·û¼¯ÎªGBK
http.setCharSet("GBK");
// Çå³ý×Ö·û¼¯ÉèÖã¬ÕâÀï´«Èënull±íʾÇå³ý×Ö·û¼¯ÉèÖÃ
http.setCharSet(null);

//13. ÉÏ´«Îļþ
// ´´½¨Ò»¸öеÄokHttp¶ÔÏóʵÀý
let http = new okHttp();
// ¶¨ÒåÒªÉÏ´«ÎļþµÄÄ¿±êURL
let url = "http://example.com/upload";
// ¶¨ÒåÉÏ´«ÎļþµÄ²ÎÊýÃû³Æ
let paramName = "file";
// ¶¨ÒåÒªÉÏ´«ÎļþµÄ±¾µØ·¾¶
let filePath = "/path/to/your/file.txt";
// ¶¨Òå¶îÍâµÄPOSTÊý¾Ý£¬ÓëÎļþÒ»ÆðÉÏ´«
let additionalData = {
    "description": "This is a test file.",
    "userId": "12345"
};
// ÉÏ´«Îļþµ½Ö¸¶¨µÄURL£¬²¢»ñÈ¡ÏìÓ¦½á¹û£¬¼ÙÉèuploadFile·½·¨ÓÃÓÚÉÏ´«Îļþ
let response = http.uploadFile(url, paramName, filePath, additionalData);
// ´òÓ¡ÉÏ´«ÎļþºóµÄÏìÓ¦½á¹û
printl(response);


»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

±¾°æ»ý·Ö¹æÔò

¹Ø±Õ

QQ|»ÓªÏúÈí¼þ×ÛºÏÌÖÂÛ|»ÓªÏúÈí¼þÓÐÎʱشð|»ÓªÏúÈí¼þ½Ì³ÌרÇø|»ÓªÏúÈí¼þPOST½Å±¾·ÖÏí|»ÓªÏúÈí¼þÆÕͨ½Å±¾·ÖÏí|»ÓªÏúÈí¼þÈí¼þ×ÊѶ|»ÓªÏúÈí¼þ¾«Æ·Èí¼þ|»ÓªÏúÈí¼þ¸üй«¸æ|ÓªÏúÈí¼þ|B2BÈí¼þ|B2BÍøÂçÈí¼þ ( ¾©ICP±¸09078825ºÅ )±¾ÍøÕ¾¿ª·¢µÄÓªÏúÈí¼þÊÇÒ»¿îеÄÍøÂçÓªÏúÈí¼þ£¬Õâ¿îÓªÏú¿ÉÒÔÈ¥ÍøÕ¾Èí¼þ£¬²©¿ÍÈí¼þ£¬B2BÈí¼þ£¬·ÖÀàÐÅÏ¢Íø·¢Ìù£¬¿ÉÒÔÇÀɳ·¢£¬¿ÉÒÔµ½°Ù¶ÈÎÄ¿âÉÏ´«WORDÎĵµ£¬¿ÉÒÔµ½Ò»Ð©ÊÇÏà²áÍøÕ¾×Ô¶¯ÉÏ´«Í¼Æ¬£¬Õâ¸ö×Ô¶¯·¢ÌûÈí¼þ×Ô´øÔÆÖ©Ö룬¼Ó¿ìÊÕ¼£¬ÓÐ6ÖÖ¶Ô½Ó´òÂë½Ó¿Ú£¬·½±ã£¬Ð§Âʸߣ¬Ëٶȿ죬¶øÇÒ¶ÔÍ϶¯µÄÑéÖ¤ÂëÈ«ÍøµÚÒ»¼Ò¶À¼ÒÖ§³Ö£¬È«²¿Ô­´´¼¼Êõ£¬¶À¼ÒÑз¢£¬Õý°æÔ­´´´ø°æȨÈí¼þ¡£Ñ¡ÔñÍòÄÜÓªÏúÈí¼þ£¬¾ÍÑ¡ÔñÁËÒ»ÖÖ׬ǮµÄЧÂÊ£¬´ÓûÓб»³¬Ô½¹ý£¬Ò»Ö±ÔÚŬÁ¦Ñз¢Ð¼¼Êõ¡£·Å·ÉÃÎÏ룬½â·ÅË«ÊÖ£¬À´µã´´Ò⣬³É¾ÍÄãµÄÃÎÏ룬¾ÍÔÚÍòÄÜÓªÏúÈí¼þ¿ªÊ¼

map2

GMT+8, 2025-1-21 14:14 , Processed in 0.146938 second(s), 29 queries .

¿ìËٻظ´ ·µ»Ø¶¥²¿ ·µ»ØÁбí