473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 23252
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

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

Similar topics

0
by: Art Perry | last post by:
I have a class that opens up an HttpsURLConnection to a web server. The purpose is to log in to the web resource using Siteminder forms based authentication. Therefore, the request is a POST, and...
5
by: maong | last post by:
Is it possible for a Java network application, using HttpURLConnection, to set or influence the setting of, http headers like Connection. One example might be setting Connection: Close to indicate...
2
by: Steve | last post by:
Does anyone know what is wrong with this code or why I might be experiencing this behavior? I have a Java application that submits HTTP POST requests to a servlet (with content in the body of...
10
by: Christopher H. Laco | last post by:
Long story longer. I need to get web user input into a backend system that a) only grocks single byte encoding, b) expectes the data transer to be 1 bytes = 1 character, and c) uses the HP Roman-6...
8
by: Demon News | last post by:
I'm trying to do a transform (Using XmlTransform class in c#) and in the Transform I'm specifying the the output xsl below: <xsl:output method="xml" encoding="UTF-8" indent="no"/> the...
4
by: fitsch | last post by:
Hi, I am trying to write a generic RSS/Atom/OPML feed client. The problem is, that those xml feeds may have different encodings: - <?xml version="1.0" encoding="ISO-8859-1" ?>... - <?xml...
2
by: velle | last post by:
My headache is growing while playing arround with unicode in Python, please help this novice. I have chosen to divide my problem into a few questions. Python 2.3.4 (#1, Feb 2 2005, 12:11:53) ...
0
by: Chris McDonough | last post by:
ElementTree's XML serialization routine implied by tree._write(file, node, encoding, namespaces looks like this (elided): def _write(self, file, node, encoding, namespaces): # write XML to file...
1
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.