pushya
pushya

Reputation: 4418

Spanish characters and URISyntaxException

I am working on a Spanish version of search and when ever the user types in the Spanish characters(say HÍBRIDOS) I see some exception(shown below). Showing how i coded below. the url is sent is over the wire is as shown.

url=http://wwwdev.searchbridg.com/absd/JSONControllerServlet.do?&N=0&Ntk=AllText&Ntt=HÃBRIDOS&Nty=1&Ntx=mode+matchall

  DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    try {
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSoTimeout(params, 10000);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    HttpHost proxy = new HttpHost(getProxy(), getProxyPort());
    ConnRouteParams.setDefaultProxy(params, proxy);
    URI uri;
    InputStream data = null;
        uri = new URI(url);
        HttpGet method = new HttpGet(uri);
        HttpResponse response=null;
        try {
        response = httpClient.execute(method);
        }catch(Exception e) {
            e.printStackTrace();
            throw e;
        }
        data = response.getEntity().getContent();
    Reader r = new InputStreamReader(data);
    HashMap<String, Object> jsonObj = (HashMap<String, Object>) GenericJSONUtil.fromJson(r);

java.net.URISyntaxException: Illegal character in query at index 101: http://wwwdev.searchbridge.com/abs/JSONControllerServlet.do?&N=0&Ntk=AllText&Ntt=H├?BRIDOS&Nty=1&Ntx=mode+matchall
    at java.net.URI$Parser.fail(URI.java:2816)
    at java.net.URI$Parser.checkChars(URI.java:2989)
    at java.net.URI$Parser.parseHierarchical(URI.java:3079)
    at java.net.URI$Parser.parse(URI.java:3021)
    at java.net.URI.<init>(URI.java:578)

I tried encoding using UTF-8 encoding and still not working shows same exception. The html pages is set to <meta charset="utf-8" />

byte[] bytes = url.getBytes("UTF8");
    String stringuRL = new String(bytes,"UTF-8");
        uri = new URI(stringuRL);

Upvotes: 2

Views: 877

Answers (2)

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

All parameters in a get request needs to have its value encoded.

If you are using HTTPClient 4 you can do this more or less like this:

List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("parameter_name_Ã", "another value with ~ãé"));
parameters.add(new BasicNameValuePair("second_parameter", "still other ú û"));
String url = "http://foo.bar/?" + URLEncodedUtils.format(parameters, "UTF-8");

The result on this case will be http://foo.bar/?parameter_name_%C3%83=another+value+with+%7E%C3%A3%C3%A9&second_parameter=still+other+%C3%BA+%C3%BB

Upvotes: 1

Alfabravo
Alfabravo

Reputation: 7589

If you're sending special chars on request (GET request), you must URLescape them. Look at this thread to find out how. HTTP URL Address Encoding in Java

When you receive the request, you must do the reverse process to get the original word.

Upvotes: 4

Related Questions