在默认浏览器中打开网页,网址中带有双引号(“) [英] Opening webpage in default browser with double-quotes (") inside url
问题描述
当我尝试打开链接中带有双引号(
)的任何网站时,例如 user.php?name = stackoverflow
只会削减
或有时将我重定向到Google!
使用的代码:
When I try to open any site that has double-quotes ("
) inside the link , for ex. user.php?name="stackoverflow"
it just cuts "
or sometimes it redirects me to Google!?
Used code:
ShellExecute(0, 'open', PChar('open'), PChar(URL), nil, SW_SHOW) ;
推荐答案
您需要使用完全限定的 URL
包括 http://
并通过替换 URL
转义/编码带有%22
的双引号(
)。
You need use a fully qualified URL
including the http://
and escape/encode the URL
by replacing the double-quotes ("
) with %22
.
也传递了错误的参数。
请参见MSDN:使用ShellExecute启动默认的Web浏览器
示例:
procedure TForm1.Button1Click(Sender: TObject);
var
URL: string;
begin
URL := 'http://www.user.com/?name="stackoverflow"';
URL := StringReplace(URL, '"', '%22', [rfReplaceAll]);
ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
end;
您应该始终对URL参数进行编码,而不仅仅是双引号。您可以将Indy与 TIdURI.URLEncode
- IdURI
单元一起使用。
您也可以使用< HTTPApp
单元中的code> HTTPEncode 在 URL
中对每个参数进行编码。
You should always encode the URL parameters, not only double-quotes. You can use Indy with TIdURI.URLEncode
- IdURI
unit.
You could also use HTTPEncode
from the HTTPApp
unit to encode each parameter in the URL
.
请注意, TIdURI.URLEncode
将对进行编码?
以及&
分隔符。所以我认为最好使用 HTTPEncode
对每个参数进行编码,例如:
Note that TIdURI.URLEncode
will encode the ?
and the &
separators also. so I think it's a better idea to encode each parameter separately with HTTPEncode
e.g:
URL := 'http://www.user.com/?param1=%s¶m2=%s';
URL := Format(URL, [
HTTPEncode('"stackoverflow.com"'),
HTTPEncode('hello word!')]);
// output: http://www.user.com/?param1=%22stackoverflow.com%22¶m2=hello+word!
这篇关于在默认浏览器中打开网页,网址中带有双引号(“)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!