在 Apache 的 httpclient 上向 HttpPost 添加参数

发布于 2025-01-07 11:20:44 字数 295 浏览 1 评论 0原文

我正在尝试在 HttpPost 对象中设置一些 Http 参数。

HttpPost post=new HttpPost(url);
HttpParams params=new BasicHttpParams();
params.setParameter("param", "value");
post.setParams(params);
HttpResponse response = client.execute(post);

看起来参数根本没有设置。您知道为什么会发生这种情况吗?

谢谢

I am trying to set some Http parameters in the HttpPost object.

HttpPost post=new HttpPost(url);
HttpParams params=new BasicHttpParams();
params.setParameter("param", "value");
post.setParams(params);
HttpResponse response = client.execute(post);

It looks like the parameter is not set at all. Do you have any idea why this is happening?

Thank you

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

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

发布评论

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

评论(2

孤独患者 2025-01-14 11:20:44

对于那些希望使用 HttpGet 找到答案的人,这里有一个(来自 https://stackoverflow.com/a/4660576/330867) :

StringBuilder requestUrl = new StringBuilder("your_url");

String querystring = URLEncodedUtils.format(params, "utf-8");
requestUrl.append("?");
requestUrl.append(querystring);

HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(requestUrl.toString());

注意:这没有考虑 your_url 的状态:如果已经有一些参数,如果已经包含“?”等。我假设您知道如何编码/搜索并将根据您的情况进行调整。

For those who hopes to find the answer using HttpGet, here's one (from https://stackoverflow.com/a/4660576/330867) :

StringBuilder requestUrl = new StringBuilder("your_url");

String querystring = URLEncodedUtils.format(params, "utf-8");
requestUrl.append("?");
requestUrl.append(querystring);

HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(requestUrl.toString());

NOTE: This doesn't take in consideration the state of your_url : if there is already some parameters, if it already contains a "?", etc. I assume you know how to code/search and will adapt regarding your case.

妥活 2025-01-14 11:20:44
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文