gpt4 book ai didi

java - 尝试添加 http 参数但不起作用

转载 作者:行者123 更新时间:2023-12-01 04:48:04 26 4
gpt4 key购买 nike

HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpMethod = new HttpPost(this.transformURL(request));
BasicHttpParams params = new BasicHttpParams();
params.setParameter("name", name);
httpMethod.setParams(params);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
httpclient.execute(httpMethod, responseHandler);
}catch{
LOG.error("Error");
} finally {
httpclient.getConnectionManager().shutdown();
}

我有上面的代码,我试图传入一个名称变量作为参数,以便通过 request.getParameter("name") 在另一个方法中获取。

它似乎不起作用,当我调试时,我可以看到参数被设置,但是当我跟踪它到执行的下一个方法时,它不会获取参数。

有什么建议吗?

编辑:

我添加了这个并且效果很好

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("name", request.getParameter("name")));
httpMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));

最佳答案

你检查过this example吗? ?它使用类 BasicNameValuePair 而不是像您一样使用 BasicHttpParams

此外,documentation for the version 3.x of HttpClient是吗:

    PostMethod post = new PostMethod("http://jakarata.apache.org/");
NameValuePair[] data = {
new NameValuePair("user", "joe"),
new NameValuePair("password", "bloggs")
};
post.setRequestBody(data);
// execute method and handle any error responses.
...
InputStream in = post.getResponseBodyAsStream();
// handle response.

更新:BasicHttpParams类是HttpParams接口(interface)的实现,正如@Perception下面所指出的,它是一组属性“自定义 HTTP 客户端的行为”。来自HttpParams javadoc : “HttpParams 预计用于‘一次写入 - 多次读取’模式。一旦初始化,HTTP 参数预计不会在 HTTP 消息处理过程中发生变化。”

关于java - 尝试添加 http 参数但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15480765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com