문제

그래서 Java 를 사용하는 코드 자카르타 HttpClient 다음과 같다:

URI aURI = new URI( "http://host/index.php?title=" + title + "&action=edit" );
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery());

문제는 경우 title 을 포함한 모든 앰퍼샌드(&)간주는 매개변수 구분 기호 및 요청이 터져...우로 교체하는 URL-escaped 해당하는 %26, 다음이 가져 두 번 이스케이프 getEscapedPathQuery()로 %2526.

나는 현재 작업이 기본적으로 피해를 복구후:

URI aURI = new URI( "http://host/index.php?title=" + title.replace("&", "%26") + "&action=edit" );
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery().replace("%2526", "%26"));

하지만 거기에 하는 더 좋은 방법이 될 이를 위해,오른쪽?참고 제목이 포함될 수 있습니다 예측할 수 없는 UTF-8 자 등,그래서 탈출하는 다른 모든 것은 필수적입니다.

도움이 되었습니까?

해결책

Here you go:

import java.net.URLEncoder;
...
...
URI aURI = new URI( "http://host/index.php?title=" + URLEncoder.encode(title,"UTF-8") + "&action=edit" );
GetMethod aRequest = new GetMethod( aURI.getPathQuery());

Check java.net.URLEncoder for more info.

다른 팁

Why are you calling getEscapedPathQuery() if you don't want the escaping? Just decide who's responsibility it is and be consistent.

URLEncoder 클래스입니다.

유틸리티 클래스를 위한 HTML 형태로 인코딩이 있습니다.이 클래스가 포함되어 정 방법을 변환하기 위한 문자열을 application/x-www-form-urlencoded MIME 형식으로 되어 있습니다.자세한 내용 에 대한 HTML 형태로 인코딩하십시오 HTML 사양에 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top