简体   繁体   中英

How to guarantee a java POST request string / text to be UTF-8 encoding

I have a textmessage/string with letters like ä,ü,ß. I want everything to be UTF-8 encoded. When I write to a file or print the string to console, everything is fine. But when I want to send the same string to a web service, I get instead of ä,ü,ß the following
I read the file from a Servlet. Do I really have to use the following 2 lines to get a UTF-8 encoded text?

byte [] bray = text.getBytes("UTF-8");
                text = new String(bray);

.

public static String readAsStream_UTF8(String filePathName){
        String text ="";
        InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/"+filePathName);
        if(input == null){          
            System.out.println("Inputstream null.");            
        }else{          
            InputStreamReader isr = null;
            try {               
                isr = new InputStreamReader((InputStream)input, "UTF-8");
                BufferedReader reader = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                String sCurrentLine;
                while ((sCurrentLine = reader.readLine()) != null) {
                    sb.append(sCurrentLine);                                
                }       
                text= sb.toString();        

                //it works only if I use the following 2 lines
                byte [] bray = text.getBytes("UTF-8");
                text = new String(bray);

            } catch (Exception e1) { 
                e1.printStackTrace();
            }           
        }               
        return text;
    }

My sendPOST method looks something like the following:

String charset = "UTF-8";
OutputStreamWriter writer = null;
HttpURLConnection con = null;
String response_txt ="";
InputStream iss = null;

try {

    URL url = new URL(urlService);
    con = (HttpURLConnection)url.openConnection();
    con.setDoOutput(true); //triggers POST
    con.setDoInput(true);
    con.setRequestMethod("POST");
    con.setRequestProperty("accept-charset", charset);
    //con.setRequestProperty("Content-Type", "application/soap+xml");
    con.setRequestProperty("Content-Type", "application/soap+xml;charset=UTF-8");


    writer = new OutputStreamWriter(con.getOutputStream());
    writer.write(msg); //send POST data string      
    writer.flush();
    writer.close();

What do I have to do to force the msg, that will be sent to the web service, to really be UTF-8 encoded.

If you know the encoding of the file which you want to send you don't need to convert it to an intermediary string. Simply copy its bytes to the output:

// inputstream to a UTF-8 encoded resource file
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/"+filePathName);

HttpURLConnection con = ...

// set contenttype and encoding
con.setRequestProperty("Content-Type", "application/soap+xml;charset=UTF-8");
// copy input to output
copy(in, con.getOutputStream()); 

using some copy function.

Additionally you could also set the Content-Length header to the size of the resource file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM