HttpURLConnection encoding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gephestus
    New Member
    • Dec 2007
    • 2

    HttpURLConnection encoding

    Hello.

    I am trying to make a request via HttpURLConnecti on 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(questio n 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:
    Code:
    private Map params = new HashMap(); // This map is filled out with HttpServletRequest.getParameterMap()
    
    
    public InputStream read() throws IOException {
        	log.debug("Trying to read data from url [" + resource.toString() + "]");
            URL requestUrl = resource;
            /*open new URLConnection*/
            HttpURLConnection httpCon = (HttpURLConnection) requestUrl.openConnection();
            httpCon.setDoOutput(true);
            httpCon.setDoInput(true);
            httpCon.setUseCaches(false);
            httpCon.setRequestMethod("POST");
            /*add headers*/
            for (Iterator e = headers.keySet().iterator(); e.hasNext();) {
                String headName = e.next().toString();
                String headValue = (String)headers.get(headName);
                httpCon.setRequestProperty(headName, headValue);
                System.out.println("Setting: " + headName + " --- " + headValue);
            }
    		
            /*add parameters*/
    		httpCon.connect();
    		log.debug("Connect successful");
    		
    		StringBuffer buffer = new StringBuffer();
    		
            log.debug("----------------Map parameters--------------------");
            for (Iterator i = params.keySet().iterator(); i.hasNext(); ) {
            	String key = (String) i.next();
                log.debug("name=" + key);
            	Object value = params.get(key);
            	if (value instanceof String[]){
            		String [] arrVal = (String[]) value;
    				for (int j = 0; j < arrVal.length; j++){
                        log.debug("value=" + arrVal[j]);
    								buffer.append(key + "=" + arrVal[j] + ((j+1 < arrVal.length || i.hasNext()) ? "&" : ""));
    					log.debug("valueEncoded=" + encodeURL(arrVal[j], "UTF-8"));
            		}
    			}else{
                    log.debug("value=" + value);
    				buffer.append(key + "=" + value + (i.hasNext() ? "&" : ""));
    			}
            }
            log.debug("----------------Map parameters--------------------");
           
            /* Send request parameters */
            PrintWriter out = new PrintWriter(httpCon.getOutputStream());  
            out.write(buffer.toString());
            
            out.flush();
            out.close();
    
            /* make request */
            int responseCode = httpCon.getResponseCode();
            log.debug("Response code: " + responseCode);
            if (responseCode != 200)
                throw new IOException("Invalid HTTP response code. Response code: " + responseCode + ". Message: " + getContentFromInputStream(httpCon.getErrorStream()));
          
            return httpCon.getInputStream();
        }
    Edit: removed comments in code
  • gephestus
    New Member
    • Dec 2007
    • 2

    #2
    Adding

    Code:
    httpCon.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=windows-1251");
    has solved it for me.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by gephestus
      Adding

      Code:
      httpCon.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=windows-1251");
      has solved it for me.
      Thanks for posting back and sharing your solution.

      Comment

      • Aftabkhan24
        New Member
        • May 2014
        • 1

        #4
        well done buddy , you saved my day
        Thanx

        Comment

        Working...