【问题标题】:how to encode [ and ] while sending as a post request using apache commons httpclient in javajava - 如何在使用java中的apache commons httpclient作为发布请求发送时编码[和]
【发布时间】:2018-04-23 06:36:11
【问题描述】:

我的代码

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 库。

【问题讨论】:

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


【解决方案1】:

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

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

它会为您翻译保留的字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多