Domanda

I have a application that receive a message from SMPP server and forward the received message to users web services. all users have a URL in database like this:

http://www.example.com/get.php?from=${originator}&to=${destination}&data=${content}
http://www.example.com/submit?origin=${originator}&dst=${destination}&cnt=${content}

All of them have ${originator},${destination},${content} in their pattern. they may have other parameters in their URL or not. I use Jodd StringTemplateParser to replace above arguments after getting user URL from database:

private StringTemplateParser stp = new StringTemplateParser();
private final Map<String, String> args = new HashMap<>();

args.put("originator", msg.getOriginator());
args.put("destination", msg.getDestination());
args.put("content", msg.getContent());

String result = stp.parse(deliveryUrl, new MacroResolver() {
    @Override
    public String resolve(String macroName) {
        return args.get(macroName);
    }
});

Then I use apache http client to call user URL:

URL url = new URL(result);
int port = url.getPort();
uri = new URI(url.getProtocol(), null, url.getHost(), port == -1 ? 80 : port, url.getPath(), url.getQuery(), null);

DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = null;
try {
    request = new HttpGet(uri);
    client.execute(request)
} catch (Exception ex) {
    if (request != null) {
        request.releaseConnection();
    }
}

I tried to do some encoding because user may send any text containing characters like %$&. The problem is when content contains something like hello&x=2&c=3243 user only gets hello. URI class treats that as regular GET query parameter. Can anyone help?

È stato utile?

Soluzione

You need to encode the parameter values when you are building your new URI.

You can encode the parameters using URLEncoder.encode(String).

So, in your MacroResolver, just return URLEncoder.encode(map.get(macroName)).

Altri suggerimenti

I think in your case you need to use HTTP default content char-set something like this for your code

request = new HttpGet(url+"?"+URLEncodedUtils.format(namevaluepair, HTTP.DEFAULT_CONTENT_CHARSET));

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top