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.