การเข้ารหัส URL
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
URL จะมีอักขระบางตัวไม่ได้ (เช่น ช่องว่าง) และอักขระอื่นๆ บางตัวมีความหมายพิเศษใน URL ในแบบฟอร์ม HTML จะใช้อักขระ =
เพื่อ
แยกชื่อออกจากค่า ไวยากรณ์ทั่วไปของ URI ใช้การเข้ารหัส URL
เพื่อจัดการปัญหานี้ ขณะที่แบบฟอร์ม HTML จะทำการแทนที่เพิ่มเติม
แทนที่จะใช้การเข้ารหัสเปอร์เซ็นต์สำหรับอักขระดังกล่าวทั้งหมด
เช่น ช่องว่างในสตริงจะได้รับการเข้ารหัสด้วย %20
หรือแทนที่ด้วย
เครื่องหมายบวก (+
) หากคุณใช้เครื่องหมายไปป์ (|
) เป็นตัวคั่น โปรด
เข้ารหัสไปป์เป็น %7C
คอมมาในสตริงควรเข้ารหัสเป็น %2C
เราขอแนะนำให้คุณใช้ไลบรารีการสร้าง URL ปกติของแพลตฟอร์มเพื่อเข้ารหัส URL โดยอัตโนมัติ เพื่อให้มั่นใจว่า URL จะมีการกำหนดอักขระหลีกอย่างเหมาะสมสำหรับแพลตฟอร์มของคุณ
การสร้าง URL ที่ถูกต้อง
คุณอาจคิดว่า URL ที่ "ถูกต้อง" นั้นชัดเจนในตัวอยู่แล้ว แต่
ในความเป็นจริงแล้วไม่ใช่เช่นนั้น ตัวอย่างเช่น URL ที่ป้อนในแถบที่อยู่ของเบราว์เซอร์อาจมีอักขระพิเศษ (เช่น "上海+中國"
) เบราว์เซอร์ต้องแปลอักขระเหล่านั้นเป็นการเข้ารหัสอื่นภายในก่อนที่จะส่ง
ในทำนองเดียวกัน โค้ดใดๆ ที่สร้างหรือยอมรับอินพุต UTF-8
อาจถือว่า URL ที่มีอักขระ UTF-8 เป็น "ใช้ได้" แต่ก็จะต้อง
แปลอักขระเหล่านั้นก่อนที่จะส่งไปยังเว็บเซิร์ฟเวอร์ด้วย
กระบวนการนี้เรียกว่า
URL-encoding หรือ percent-encoding
อักขระพิเศษ
เราต้องแปลอักขระพิเศษเนื่องจาก URL ทั้งหมดต้องเป็นไปตามไวยากรณ์ที่ระบุโดยข้อกำหนดตัวระบุ
ทรัพยากรแบบสม่ำเสมอ (URI) ซึ่งหมายความว่า URL
ต้องมีเฉพาะอักขระ ASCII ที่เป็นชุดย่อยพิเศษ ได้แก่ สัญลักษณ์ตัวอักษรและตัวเลขที่คุ้นเคย
และอักขระที่สงวนไว้บางตัวสำหรับใช้เป็นอักขระควบคุม
ภายใน URL ตารางนี้สรุปอักขระดังกล่าว
สรุปอักขระที่ใช้ได้ใน URL
ตั้งค่า | อักขระ | การใช้งาน URL |
ตัวอักษรและตัวเลขคละกัน |
ก ข ค ง จ ฉ ช ซ ฌ ญ ฎ ฏ
ฐ ฑ ฒ ณ ด ต ถ ท ธ น บ ป ผ พ ภ ม
ย ร ล ว ศ ษ ส ห ฬ อ ฮ
อ 0 1 2 3 4 5 6 7 8 9
| สตริงข้อความ การใช้รูปแบบ (http ) พอร์ต (8080 ) ฯลฯ |
ไม่ได้จอง |
- _ . ~ |
สตริงข้อความ |
จองแล้ว |
! * ' ( ) ; : @ & = + $ , / ? % # [ ] |
อักขระควบคุมและ/หรือสตริงข้อความ |
เมื่อสร้าง URL ที่ถูกต้อง คุณต้องตรวจสอบว่า URL นั้นมีเฉพาะอักขระที่แสดงในตาราง
เท่านั้น การปรับ URL ให้ใช้ชุดอักขระนี้มัก
ทำให้เกิดปัญหา 2 อย่าง ได้แก่ ปัญหาการละเว้นและปัญหาการแทนที่
- อักขระที่คุณต้องการจัดการอยู่นอกชุดอักขระ
ที่ตั้งไว้ข้างต้น เช่น อักขระในภาษาต่างประเทศ
เช่น
上海+中國
ต้องได้รับการเข้ารหัสโดยใช้อักขระ
ด้านบน โดยทั่วไปแล้ว ช่องว่าง (ซึ่งไม่อนุญาตให้ใช้ใน URL) มักจะแสดงโดยใช้อักขระบวก '+'
ด้วย
- อักขระจะอยู่ในชุดด้านบนเป็นอักขระที่สงวนไว้
แต่ต้องใช้ตามตัวอักษร
เช่น
?
ใช้ภายใน URL เพื่อระบุจุดเริ่มต้นของสตริงคำค้นหา หากต้องการใช้สตริง "? and the Mysterions" คุณจะต้องเข้ารหัสอักขระ '?'
อักขระทั้งหมดที่จะเข้ารหัส URL จะได้รับการเข้ารหัส
โดยใช้'%'
และค่าฐานสิบหก 2 อักขระ
ที่สอดคล้องกับอักขระ UTF-8 เช่น
上海+中國
ใน UTF-8 จะได้รับการเข้ารหัส URL เป็น
%E4%B8%8A%E6%B5%B7%2B%E4%B8%AD%E5%9C%8B
สตริง
? and the Mysterians
จะได้รับการเข้ารหัส URL เป็น
%3F+and+the+Mysterians
หรือ %3F%20and%20the%20Mysterians
อักขระทั่วไปที่ต้องเข้ารหัส
อักขระทั่วไปบางตัวที่ต้องเข้ารหัส ได้แก่
อักขระที่ไม่ปลอดภัย |
ค่าที่เข้ารหัส |
Space |
%20 |
" |
%22 |
< |
%3C |
> |
%3E |
# |
%23 |
% |
%25 |
| |
%7C |
บางครั้งการแปลง URL ที่คุณได้รับจากข้อมูลที่ผู้ใช้ป้อนอาจเป็นเรื่อง
ยาก เช่น ผู้ใช้อาจป้อนที่อยู่เป็น "5th&Main St."
โดยทั่วไป คุณควรสร้าง URL จากส่วนต่างๆ โดยถือว่า
อินพุตของผู้ใช้เป็นอักขระตามตัว
นอกจากนี้ URL ยังจำกัดไว้ที่ 16384 อักขระสำหรับบริการเว็บทั้งหมดของ Google Maps Platform
และเว็บ API แบบคงที่ สำหรับบริการส่วนใหญ่ คุณแทบจะไม่ต้องกังวลเรื่องจำนวนอักขระสูงสุดนี้ อย่างไรก็ตาม โปรดทราบว่าบริการบางอย่างมีพารามิเตอร์หลายรายการที่อาจส่งผลให้ URL ยาว
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eURLs can only contain a specific set of ASCII characters and some reserved characters, requiring encoding of other characters.\u003c/p\u003e\n"],["\u003cp\u003eURL encoding involves replacing unsafe characters with a '%' followed by their two-digit hexadecimal UTF-8 representation.\u003c/p\u003e\n"],["\u003cp\u003eCharacters like spaces, quotation marks, less than, greater than, hash, percent, and pipe often require URL encoding.\u003c/p\u003e\n"],["\u003cp\u003eBuilding URLs programmatically using platform libraries is recommended for automatic and proper encoding.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Maps Platform web services and static web APIs have a URL character limit of 16384.\u003c/p\u003e\n"]]],["URLs require encoding because they only accept a specific subset of ASCII characters. Special characters, like spaces and foreign language characters, must be encoded using percent encoding (e.g., `%20` for space) or plus signs in specific instances. Reserved characters, such as `=`, `?`, and `|`, also need encoding when used literally, not as control characters. It's best to utilize platform URL building libraries for automatic encoding, guaranteeing proper escaping, because it can be complex to do manually. It is also important to note that URLs are limited to 16384 characters for Google Maps Platform web services.\n"],null,["Some characters cannot be part of a URL (for example, the space) and some other\ncharacters have a special meaning in a URL. In HTML forms, the character `=` is\nused to separate a name from a value. The URI generic syntax uses URL encoding\nto deal with this problem, while HTML forms make some additional substitutions\nrather than applying percent encoding for all such characters.\n\nFor example, spaces in a string are either encoded with `%20` or replaced with\nthe plus sign (`+`). If you use a pipe character (`|`) as a separator, be sure\nto encode the pipe as `%7C`. A comma in a string should be encoded as `%2C`.\n\nIt is recommended you use your platform's normal URL building libraries to\nautomatically encode your URLs, to ensure the URLs are properly escaped for your\nplatform.\n\nBuilding a valid URL\n\nYou may think that a \"valid\" URL is self-evident, but\nthat's not quite the case. A URL entered within an address bar in a\nbrowser, for example, may contain special characters (e.g.\n`\"上海+中國\"`); the browser needs to internally translate\nthose characters into a different encoding before transmission.\nBy the same token, any code that generates or accepts UTF-8 input\nmight treat URLs with UTF-8 characters as \"valid\", but would also need\nto translate those characters before sending them out to a web server.\nThis process is called [URL-encoding](https://en.wikipedia.org/wiki/Query_string#URL_encoding) or [percent-encoding](https://en.wikipedia.org/wiki/Percent-encoding).\n\nSpecial characters\n\nWe need to translate special characters because\nall URLs need to conform to the syntax specified by the\n[Uniform\nResource Identifier (URI)](http://tools.ietf.org/html/rfc3986) specification. In effect, this means that URLs\nmust contain only a special subset of ASCII characters: the familiar\nalphanumeric symbols, and some reserved characters for use as control\ncharacters within URLs. This table summarizes these characters:\nSummary of Valid URL Characters\n\n| Set | characters | URL usage |\n|--------------|-----------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------|\n| Alphanumeric | a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 | Text strings, scheme usage (`http`), port (`8080`), etc. |\n| Unreserved | - _ . \\~ | Text strings |\n| Reserved | ! \\* ' ( ) ; : @ \\& = + $ , / ? % # \\[ \\] | Control characters and/or Text Strings |\n\nWhen building a valid URL, you must ensure that it contains only those characters shown in the\ntable. Conforming a URL to use this set of characters generally\nleads to two issues, one of omission and one of substitution:\n\n- Characters that you wish to handle exist outside of the above set. For example, characters in foreign languages such as `上海+中國` need to be encoded using the above characters. By popular convention, spaces (which are not allowed within URLs) are often represented using the plus `'+'` character as well.\n- Characters exist within the above set as reserved characters, but need to be used literally. For example, `?` is used within URLs to indicate the beginning of the query string; if you wish to use the string \"? and the Mysterions,\" you'd need to encode the `'?'` character.\n\nAll characters to be URL-encoded are encoded\nusing a `'%'` character and a two-character hex\nvalue corresponding to their UTF-8 character. For example,\n`上海+中國` in UTF-8 would be URL-encoded as\n`%E4%B8%8A%E6%B5%B7%2B%E4%B8%AD%E5%9C%8B`. The\nstring `? and the Mysterians` would be URL-encoded as\n`%3F+and+the+Mysterians` or `%3F%20and%20the%20Mysterians`.\n\nCommon characters that need encoding\n\nSome common characters that must be encoded are:\n\n| Unsafe character | Encoded value |\n|------------------|---------------|\n| Space | `%20` |\n| \" | `%22` |\n| \\\u003c | `%3C` |\n| \\\u003e | `%3E` |\n| # | `%23` |\n| % | `%25` |\n| \\| | `%7C` |\n\nConverting a URL that you receive from user input is sometimes\ntricky. For example, a user may enter an address as \"5th\\&Main St.\"\nGenerally, you should construct your URL from its parts, treating\nany user input as literal characters.\n\nAdditionally, URLs are limited to 16384 characters for all Google Maps Platform web services\nand static web APIs. For most services, this character limit will seldom be approached. However,\nnote that certain services have several parameters that may result in long URLs."]]