473,394 Members | 1,739 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,394 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 23266
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.