gpt4 book ai didi

post - Apache HTTP 客户端,POST 请求。如何正确设置请求参数?

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

我正在使用 Apache HTTP 客户端,我需要向我的 servlet 发送一个 POST 请求。
发送请求时,我的 servlet 没有收到任何参数(在 HttpServletRequest 中)。

这是客户端程序代码:

// Engage the HTTP client
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
HttpPost httpPost = new HttpPost("http://localhost:8080/test-json-web/JSONReceiverServlet");

// Setup the request parameters
HttpParams params = new BasicHttpParams();
params.setParameter("taskdef", task1JsonString);
httpPost.setParams(params);

// Make the request
response = httpclient.execute(httpPost);

HttpEntity responseEntity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if(responseEntity != null) {
System.out.println("Response content length: " + responseEntity.getContentLength());
}

String jsonResultString = EntityUtils.toString(responseEntity);
EntityUtils.consume(responseEntity);
System.out.println("----------------------------------------");
System.out.println("result:");
System.out.println();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}

如何正确设置 POST 请求参数以便 servlet 实际接收它们?

最佳答案

尝试这个:

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("IDToken1", "username"));
nvps.add(new BasicNameValuePair("IDToken2", "password"));

httPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

关于post - Apache HTTP 客户端,POST 请求。如何正确设置请求参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7068346/

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