PostMethod网址参数

时间:2015-05-04 17:42:50

标签: java http-post httpclient apache-httpclient-4.x url-encoding

是否有使用PostMethod传递URL参数的替代方法?在此之后,需要将XML与URL一起发布。 由于它是一个post请求,因此URL参数应该在Request主体中传递,并且不应该是可见的。 可以使用addParameter方法吗?

网址 - http://mytest.com?abc=xyz&token=aisk%2s

1)

//this works ( no utf-8 encoding)
PostMethod pm =new PostMethod("http://mytest.com");
pm.setQueryString("abc=xyz");
pm.setQueryString("token=aisk%2s");

2)

// it encodes utf-8 and fails
PostMethod pm =new PostMethod("http://mytest.com");
NameValuePair [] nvp= new NameValuePair[2];
nvp[0]=new NameValuePair("abc","xyz");
nvp[1]=new NameValuePair("token","aisk%2s");
//encodes the token value as aisk%252s
pm.setQueryString(nvp);

在设置上述网址参数后,需要发布XML。

pm.setRequestEntity(new StringRequestEntity(xml, "application/xml", "UTF-8"));

1 个答案:

答案 0 :(得分:0)

如何使用部件而不是将参数添加到网址?

您也可以添加各种数据类型:

// using **MultipartEntity**

multipartContent.addPart("user", new StringBody("admin", ContentType.TEXT_PLAIN));
multipartContent.addPart("content", new InputStreamBody(new FileInputStream(file), ContentType.DEFAULT_BINARY));

当然,您可以获取一个页面,作为您的请求的状态。

在您的服务器上,将它们作为$ _REQUEST / $ _POST数组中的变量进行处理。

使用

var_dump($_REQUEST)

查看内容。