469,285 Members | 2,517 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,285 developers. It's quick & easy.

HttpURLConnection encoding

Hello.

I am trying to make a request via HttpURLConnection which is then handled by a certain struts Action. I construct name-value pairs for request URL by iterating through provided request parameters. The problem is that original request contains some parameters written in Cyrillic and after new request is sent out, in the struts Action responsible for handling this request, i get garbage(question marks, squares etc.) instead of parameters written in Cyrillic.


Note:Encoding parameters for the new URL with UTF-8 does not have any result. Parameters written in cyrillic are displayed correctly in the scope of this method.

Did anybody encounter similar problem and if so, how did you solve it?

Thanks in advance.

Here is the code:
Expand|Select|Wrap|Line Numbers
  1. private Map params = new HashMap(); // This map is filled out with HttpServletRequest.getParameterMap()
  2.  
  3.  
  4. public InputStream read() throws IOException {
  5.         log.debug("Trying to read data from url [" + resource.toString() + "]");
  6.         URL requestUrl = resource;
  7.         /*open new URLConnection*/
  8.         HttpURLConnection httpCon = (HttpURLConnection) requestUrl.openConnection();
  9.         httpCon.setDoOutput(true);
  10.         httpCon.setDoInput(true);
  11.         httpCon.setUseCaches(false);
  12.         httpCon.setRequestMethod("POST");
  13.         /*add headers*/
  14.         for (Iterator e = headers.keySet().iterator(); e.hasNext();) {
  15.             String headName = e.next().toString();
  16.             String headValue = (String)headers.get(headName);
  17.             httpCon.setRequestProperty(headName, headValue);
  18.             System.out.println("Setting: " + headName + " --- " + headValue);
  19.         }
  20.  
  21.         /*add parameters*/
  22.         httpCon.connect();
  23.         log.debug("Connect successful");
  24.  
  25.         StringBuffer buffer = new StringBuffer();
  26.  
  27.         log.debug("----------------Map parameters--------------------");
  28.         for (Iterator i = params.keySet().iterator(); i.hasNext(); ) {
  29.             String key = (String) i.next();
  30.             log.debug("name=" + key);
  31.             Object value = params.get(key);
  32.             if (value instanceof String[]){
  33.                 String [] arrVal = (String[]) value;
  34.                 for (int j = 0; j < arrVal.length; j++){
  35.                     log.debug("value=" + arrVal[j]);
  36.                                 buffer.append(key + "=" + arrVal[j] + ((j+1 < arrVal.length || i.hasNext()) ? "&" : ""));
  37.                     log.debug("valueEncoded=" + encodeURL(arrVal[j], "UTF-8"));
  38.                 }
  39.             }else{
  40.                 log.debug("value=" + value);
  41.                 buffer.append(key + "=" + value + (i.hasNext() ? "&" : ""));
  42.             }
  43.         }
  44.         log.debug("----------------Map parameters--------------------");
  45.  
  46.         /* Send request parameters */
  47.         PrintWriter out = new PrintWriter(httpCon.getOutputStream());  
  48.         out.write(buffer.toString());
  49.  
  50.         out.flush();
  51.         out.close();
  52.  
  53.         /* make request */
  54.         int responseCode = httpCon.getResponseCode();
  55.         log.debug("Response code: " + responseCode);
  56.         if (responseCode != 200)
  57.             throw new IOException("Invalid HTTP response code. Response code: " + responseCode + ". Message: " + getContentFromInputStream(httpCon.getErrorStream()));
  58.  
  59.         return httpCon.getInputStream();
  60.     }
  61.  
Edit: removed comments in code
Dec 19 '07 #1
3 22590
Adding

Expand|Select|Wrap|Line Numbers
  1. httpCon.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=windows-1251");
  2.  
has solved it for me.
Dec 20 '07 #2
r035198x
13,262 8TB
Adding

Expand|Select|Wrap|Line Numbers
  1. httpCon.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=windows-1251");
  2.  
has solved it for me.
Thanks for posting back and sharing your solution.
Dec 20 '07 #3
well done buddy , you saved my day
Thanx
May 26 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Art Perry | last post: by
10 posts views Thread by Christopher H. Laco | last post: by
8 posts views Thread by Demon News | last post: by
4 posts views Thread by fitsch | last post: by
reply views Thread by Chris McDonough | last post: by
1 post views Thread by ujjwaltrivedi | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.