简体   繁体   English

URL编码/解码在Web服务调用中如何工作

[英]How does url encoding/decoding work in web service calls

I need to consume a third party api which is expecting url as below 我需要使用需要以下网址的第三方api
URL?queryParam=02/10/1990 URL?queryParam = 02/10/1990年

when i am calling that api by appending queryParam(ab above) it is working fine. 当我通过附加queryParam(ab以上)调用该api时,它工作正常。

But when I call api as below 但是当我如下调用api时
GetMethod httpMethod = new GetMethod(url); GetMethod httpMethod =新的GetMethod(url); httpMethod.setQueryString(getNameValuePairs(requestParams)); httpMethod.setQueryString(getNameValuePairs(requestParams)); where requestParams is map of request params. 其中requestParams是请求参数的映射。 api is giving 400 error. api给出400错误。

api I am using here is apache.common apis 我在这里使用的api是apache.common api

when I looked in setQueryString code,requestParams are url encoded. 当我查看setQueryString代码时,requestParams是url编码的。

so my doubts are. 所以我的疑问是。

  1. Does queryParams are url encoded by default?it yes, Here apache api is encoding request param at consumer end,does consumer has to manually decode it? 是,queryParams是否默认为url编码?是的,这里apache api在使用者端编码请求参数,使用者必须手动对其进行解码吗?
  2. When api is called from browser by appending queryParams does browser also do url encoding by itself? 当通过附加queryParams从浏览器调用api时,浏览器是否也会自行进行url编码?
  3. What is the clean solution of it? 干净的解决方案是什么?

A simple solution is to frame the uri and pass it to getMethod() constructor. 一个简单的解决方案是对uri进行框架化并将其传递给getMethod()构造函数。

string url;
url = baseURL+"?queryParam=02/10/1990" ;

GetMethod httpMethod = new GetMethod(url); 

Query params are mostly used for post Request payload. 查询参数主要用于请求后的有效负载。 In your case , there's on need fo 在您的情况下,需要

Use base64 url encoding and decoding. 使用base64 url​​编码和解码。 It will solve the problem. 它将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM