如何将 HTML 作为 GET-Request 参数发送?

发布于 2024-07-18 09:10:52 字数 552 浏览 3 评论 0原文

我想使用 Apaches HttpClient 发送一个带有 GET 请求的 html 字符串:

http://sample.com/?html=<html><head>...

目前这不起作用,我认为它是一个编码问题。 您有什么想法如何做到这一点吗?

method.setQueryString(new NameValuePair[] {new NameValuePair("report", "<html>....")});
client.executeMethod(method)

此操作失败,并显示 org.apache.commons.httpclient.NoHttpResponseException:服务器本地主机无法响应。 如果我用“test..”替换 "" ,它就可以正常工作。

编辑

这似乎是编码后 URL 长度的问题,服务器不会排除这么长的 UR。 通过 POST 发送即可解决问题。

I would like to send a html string with a GET request like this with Apaches HttpClient:

http://sample.com/?html=<html><head>...

This doesnt work at the moment, i think its an encoding problem. Do you have any ideas how to do that?

method.setQueryString(new NameValuePair[] {new NameValuePair("report", "<html>....")});
client.executeMethod(method)

This fails with org.apache.commons.httpclient.NoHttpResponseException: The server localhost failed to respond. If i replace "<html>" by "test.." it works fine.

EDIT

It seams to be a problem of URL length after encoding, the server doesnt except such long URls. Sending it as POST solves the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-07-25 09:10:52

首先尝试使用 URL 编码来格式化您的 html 字符串。

String yourHtmlString = java.net.URLEncoder.encode("<html>....");
method.setQueryString(new NameValuePair[] {new NameValuePair("report", yourHtmlString)});

Try using URL Encoding to format your html string first.

String yourHtmlString = java.net.URLEncoder.encode("<html>....");
method.setQueryString(new NameValuePair[] {new NameValuePair("report", yourHtmlString)});
烟酉 2024-07-25 09:10:52

我会使用 base64 编码,并且可能会在它之前进行某种压缩,具体取决于给定内容的长度:

RFC 2068 规定:
服务器应谨慎依赖超过 255 字节的 URI 长度,因为某些较旧的客户端或代理实现可能无法正确支持这些长度。
URL 长度规范并未规定最小或最大 URL 长度,但实现方式因浏览器而异。 在 Windows 上:Opera 支持约 4050 个字符,IE 4.0+ 支持 2083 个字符,Netscape 3 -> 2083 个字符。 4.78 在导致关闭错误之前支持最多 8192 个字符,Netscape 6 在导致启动错误之前支持 ~2000 个字符。

I'd go with base64 encoding and maybe some sort of compression before it depending on the length of your content given:

RFC 2068 states:
Servers should be cautious about depending on URI lengths above 255 bytes, because some older client or proxy implementations may not properly support these lengths.
The spec for URL length does not dictate a minimum or maximum URL length, but implementation varies by browser. On Windows: Opera supports ~4050 characters, IE 4.0+ supports exactly 2083 characters, Netscape 3 -> 4.78 support up to 8192 characters before causing errors on shut-down, and Netscape 6 supports ~2000 before causing errors on start-up.

绾颜 2024-07-25 09:10:52

HTML 字符串包含应进行 URL 编码的字符。 请阅读此处

您可以使用 UrlUtils.simpleFormUrlEncode

HTML strings contain characters that should be URL encoded. Read here.

You could do the encoding with UrlUtils.simpleFormUrlEncode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文