An warware: ษ“oye kirtani url

Tabbas, bari mu fara da tsarin labarin akan adireshin URL a Java.

Rufin URL, wanda kuma aka sani da kashi-kashi, hanya ce ta rufaffen bayanai a cikin mai gano albarkatu iri ษ—aya (URI) ฦ™arฦ™ashin takamaiman yanayi. Yawancin lokaci ana amfani da shi a cikin layin tambaya ko sassan URL. Rufewa yana sa abun cikin URL ya fi aminci ta hanyar canza haruffan da ba haruffa ba zuwa tsarin da za'a iya yadawa ta Intanet.

Rufe URL ya ฦ™unshi maye gurbin haruffan ASCII marasa aminci tare da "%" tare da lambobi hexadecimal biyu. Ana maye gurbin sarari da ko dai alamar ฦ™ari "+" ko tare da "%20". A cikin Java, ana iya samun wannan ta amfani da ajin URLEncoder wanda ke ba da hanyar rufaffen rufaffen kirtani.

public class Main{

  public static void main(String[] args) {
     String url = "https://www.example.com?param=Hello World";
     String encoded = URLEncoder.encode(url, "UTF-8");
     System.out.println(encoded);  
  }
}

Java URLEncoder Class

The Java URLEncoder class wani bangare ne na kunshin java.net. Wannan aji yana ba da tsayayyen hanya mai suna encode(), wanda za'a iya amfani dashi don ษ“oye duk haruffan da basu dace ba a cikin kirtani don amfani da su a URL.

Hanyar URLEncoder.encode() tana ษ—aukar sigogi biyu:
1. Zaren URL: igiyar da za a saka.
2. Rufin haruffa: tsarin tsarin da za a yi amfani da shi.

Bayan shigar da bayanai, hanyar tana dawo da kirtani a cikin tsarin da aka rufaffen.

Bayanin lambar mataki-mataki

โ€“ Na farko, mun fara a Zaren URL wanda ya ฦ™unshi wasu haruffa marasa aminci, kamar sarari.
- Na gaba, muna kiran hanyar ษ“oye () na ajin URLEncoder. Mun wuce kirtani URL da tsarin ษ“oye (a wannan yanayin, "UTF-8") a matsayin sigogi.
- Hanyar ษ“oye () tana ษ“oye URL ษ—in mu kuma ta dawo da URL ษ—in da aka ษ“oye, wanda muke adanawa a cikin ma'auni na "incoded".
โ€“ A ฦ™arshe, mun buga fitar da โ€œencodedโ€ m. Sakamakon fitowar ya daina ฦ™unshe da wasu haruffa marasa aminci.

Makamantan ษ—akunan karatu ko ayyuka

Baya ga ginanniyar URLEncoder na Java, akwai kuma wasu ษ—akunan karatu da ayyuka da yawa don ษ“oye URL a cikin harsunan shirye-shirye daban-daban. Misali:

  • encodeURIComponent() a cikin JavaScript
  • ulencode () a cikin PHP
  • quote() a cikin tsarin urllib na Python

Waษ—annan ayyuka suna aiki daidai da URLEncoder na Java. Suna maye gurbin haruffan ASCII marasa aminci tare da "%" tare da lambobi hexadecimal biyu da sarari tare da ko dai alamar ฦ™ari "+" ko "% 20".

a ฦ™arshe, Rufin URL shine muhimmin tsari lokacin aiki tare da URLs. Yana tabbatar da cewa URL ษ—in yana da aminci don amfani da shi akan gidan yanar gizo kuma ba zai karya kowane ma'auni na intanet ba. Kuma ko da yake harsunan shirye-shirye daban-daban na iya samun ฦดan bambance-bambance a yadda suke sarrafa rubutun URL, babban ra'ayi ya tsaya iri ษ—aya. Koyaushe ka tabbata ka ษ“oye ษ“angarori na URL ษ—inka waษ—anda ke buฦ™atar ษ“oyewa.

Shafi posts:

Leave a Comment