공백 문자를 인코딩하는 URL : + 또는 % 20?
URL의 공백은 언제로 인코딩 +
되고 언제로 인코딩 %20
됩니까?
에서 위키 백과 (강조 링크 추가)
HTML 양식에 입력 된 데이터가 제출되면 양식 필드 이름과 값이 인코딩되고 GET 또는 POST 메소드를 사용하거나 과거에는 이메일을 통해 HTTP 요청 메시지로 서버로 전송됩니다. 기본적으로 사용되는 인코딩 은 개행 정규화 및 공백을 "% 20"대신 "+"로 대체하는 것과 같은 여러 수정 사항이 포함 된 일반 URI 백분율 인코딩 규칙의 초기 버전을 기반으로합니다 . 이러한 방식으로 인코딩 된 MIME 유형의 데이터는 application / x-www-form-urlencoded이며 현재 HTML 및 XForms 사양에 정의되어 있습니다 (아직도 매우 오래된 방식).
따라서, 실제 사용 퍼센트 인코딩 %20
된 URL 형태의 데이터가 사용하는 동안 변형 된 형태이다 +
. 따라서 +
쿼리 문자열의 URL 에서만 ?
.
이러한 혼란은 URL이 오늘날까지도 여전히 '파손'되어 있기 때문입니다.
예를 들어 " http://www.google.com "을 사용 하십시오 . 이것은 URL입니다. URL은 Uniform Resource Locator이며 실제로 웹 페이지에 대한 포인터입니다 (대부분의 경우). URL은 실제로 1994 년 첫 번째 사양 이후 매우 잘 정의 된 구조를 가지고 있습니다.
" http://www.google.com "URL 에 대한 자세한 정보를 추출 할 수 있습니다 .
+---------------+-------------------+
| Part | Data |
+---------------+-------------------+
| Scheme | http |
| Host | www.google.com |
+---------------+-------------------+
다음과 같이 더 복잡한 URL을 살펴보면
" https : // bob : bobby@www.lunatech.com : 8080 / file; p = 1? q = 2 # third "
다음 정보를 추출 할 수 있습니다.
+-------------------+---------------------+
| Part | Data |
+-------------------+---------------------+
| Scheme | https |
| User | bob |
| Password | bobby |
| Host | www.lunatech.com |
| Port | 8080 |
| Path | /file;p=1 |
| Path parameter | p=1 |
| Query | q=2 |
| Fragment | third |
+-------------------+---------------------+
https://bob:bobby@www.lunatech.com:8080/file;p=1?q=2#third
\___/ \_/ \___/ \______________/ \__/\_______/ \_/ \___/
| | | | | | \_/ | |
Scheme User Password Host Port Path | | Fragment
\_____________________________/ | Query
| Path parameter
Authority
예약 된 문자는 각 부분마다 다릅니다.
HTTP URL의 경우 경로 조각 부분의 공백은 "% 20"(절대적으로 "+"가 아님)으로 인코딩되어야하며 경로 조각 부분의 "+"문자는 인코딩되지 않은 상태로 둘 수 있습니다.
이제 쿼리 부분에서 공백은 "+"(이전 버전과의 호환성을 위해 : URI 표준에서 검색하지 마십시오) 또는 "% 20"으로 인코딩되고 "+"문자 (이 모호함의 결과)로 인코딩 될 수 있습니다. )는 "% 2B"로 이스케이프해야합니다.
즉, "파란색 + 하늘색"문자열은 경로 및 쿼리 부분에서 다르게 인코딩되어야합니다.
" http://example.com/blue+light%20blue?blue%2Blight+blue ".
From there you can deduce that encoding a fully constructed URL is impossible without a syntactical awareness of the URL structure.
This boils down to:
You should have %20
before the ?
and +
after.
I would recommend %20
.
Are you hard-coding them?
This is not very consistent across languages, though. If I'm not mistaken, in PHP urlencode()
treats spaces as +
whereas Python's urlencode()
treats them as %20
.
EDIT:
It seems I'm mistaken. Python's urlencode()
(at least in 2.7.2) uses quote_plus()
instead of quote()
and thus encodes spaces as "+". It seems also that the W3C recommendation is the "+" as per here: http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
And in fact, you can follow this interesting debate on Python's own issue tracker about what to use to encode spaces: http://bugs.python.org/issue13866.
EDIT #2:
I understand that the most common way of encoding " " is as "+", but just a note, it may be just me, but I find this a bit confusing:
import urllib
print(urllib.urlencode({' ' : '+ '})
>>> '+=%2B+'
A space may only be encoded to "+" in the "application/x-www-form-urlencoded" content-type key-value pairs query part of an URL. In my opinion, this is a MAY, not a MUST. In the rest of URLs, it is encoded as %20.
In my opinion, it's better to always encode spaces as %20, not as "+", even in the query part of an URL, because it is the HTML specification (RFC-1866) that specified that space characters should be encoded as "+" in "application/x-www-form-urlencoded" content-type key-value pairs (see paragraph 8.2.1. subparagraph 1.)
This way of encoding form data is also given in later HTML specifications. For example, look for relevant paragraphs about application/x-www-form-urlencoded in HTML 4.01 Specification, and so on.
Here is a sample string in URL where the HTML specification allows encoding spaces as pluses: "http://example.com/over/there?name=foo+bar". So, only after "?", spaces can be replaced by pluses. In other cases, spaces should be encoded to %20. But since it's hard to correctly determine the context, it's the best practice to never encode spaces as "+".
I would recommend to percent-encode all character except "unreserved" defined in RFC-3986, p.2.3
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
The implementation depends on the programming language that you chose.
If your URL contains national characters, first encode them to UTF-8 and then percent-encode the result.
참고URL : https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20
'Development Tip' 카테고리의 다른 글
android.os.FileUriExposedException : Intent.getData ()를 통해 앱 외부에 노출 된 file : ///storage/emulated/0/test.txt (0) | 2020.09.30 |
---|---|
TypeScript 문자열을 숫자로 변환 (0) | 2020.09.30 |
HTML의 레이아웃에 테이블을 사용하지 않는 이유는 무엇입니까? (0) | 2020.09.30 |
JavaScript에서 Switch 문 여러 사례 (0) | 2020.09.30 |
강제 메이븐 업데이트 (0) | 2020.09.30 |