如何在java中使用apache commons httpclient作为post请求发送时编码[和]

时间:2018-04-23 06:36:12

标签: java http-post apache-commons-httpclient payumoney

我的代码

url = "https://www.payumoney.com/payment/payment/addPaymentSplit?merchantKey=test&merchantTransactionId=test&totalAmount=0&totalDiscount=0&jsonSplits=[{"amountToBeSettled":0,"aggregatorDiscount":0,"splitDetails":"test","CODAmount":0,"splitAmount":0,"merchantId":"0000","aggregatorCharges":0,"CODMode":0,"aggregatorSubTransactionId":"test","sellerDiscount":0}]"

CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);

我在第二行收到非法字符错误。我尝试编码[到%5B和]到%5D。仍然是同样的错误。

使用apache-commons-httpclient库。

1 个答案:

答案 0 :(得分:0)

不要重新发明轮子,使用内置的https://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html

"...&jsonSplits=" + URLEncoder.encode("[{...", "UTF-8");

它为您翻译保留字符。