tags:

views:

550

answers:

3

Hi %,

in order to 'feed' a .NET web service from java I do pass xml strings via a direct socket connection over to the server.

Everything works wunderbar as long as I don't include any 'wierd' characters in my xml strings. Ä or ß for examples sake.

I scripted around and figured that in php5 the problem is solved by utf8_encode(myXmlString). Sadly

retString = new String (retString.getBytes(),"UTF-8");

does not work out.

Any hints would be appreciated.

thx in advance

      A
A: 

Don't forget that you have 2 levels of encoding here:

  1. the encoding of your XML document. Does that have the correct encoding (e.g. UTF-8). Can you write it to a file prior to sending to your server, and verify that it's encoded correctly ?
  2. the encoding of the stringified XML document down the wire. Again, you will need to check that. Do you get the string and then perform getBytes(String encoding) on it ? You should use a specified encoding for this, and not the deprecated defaulted implementation
Brian Agnew
1.: I can write my XML to a file, it's correct there.2.: I'm setting the Content type to "application/x-www-form-urlencoded;charset UTF-8", since the web service does not respond if i don't do so. (unfortunatly I have no access to that system)--Is there no approx equivalent to php's utf8_encode? sry for lame'in around...
KB22
+1  A: 

If your XML is correctly encoded, you shouldn't have a problem. My guess is that your XML isn't correct to start with. Rather than working round that, I'd strongly encourage you to fix everything to produce and consume the correct values.

In particular, your retString should already have the correct Unicode values. If it doesn't, you're going to run into problems whatever you do. If it does have the right values, you should be able to just convert it into bytes using the UTF-8 charset, and feed those to the socket - so long as the XML declares itself as being in UTF-8 to start with. (It will default to UTF-8 if you don't specify anything else, so long as it doesn't start with a UTF-16 byte order mark.)

I suggest you have a look at my Debugging Unicode Problems article: check the data at every step, not just by printing out the string but by looking at the individual codepoints within it. Do that at both the Java and .NET sides.

Jon Skeet
A: 

package com.tibco;

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.apache.commons.httpclient.URI; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIUtils; import org.apache.http.cookie.Cookie; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils;

public class WcfWrapper{

public static void callWcf(String getContents) throws Exception { InputStream responsexml=null; DefaultHttpClient httpclient = new DefaultHttpClient();

    java.net.URI uri = URIUtils.createURI("http", "servername", -1, "/LBSRA/ServiceName.svc/RequestPOST",
                    "tibcourl="+URLEncoder.encode("<?xml version=\"1.0\" encoding=\"utf-8\"?><ServiceConfigurations xmlns:xsi=\"http://www.w3.org some ex pl document ",HTTP.UTF_8)+"&"
                    +"rawXML="+URLEncoder.encode("<?xml version=\"1.0\" encoding=\"utf-8\"?>some xml document",HTTP.UTF_8)
               , null);
 HttpPost httpost = new HttpPost(uri);
    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("rawXML", getContents));
    httpclient.getParams().setParameter("http.socket.timeout", new
      Integer(55000));
    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    HttpResponse response = httpclient.execute(httpost);
    BufferedHttpEntity entity = new BufferedHttpEntity(response.getEntity());
    System.out.println("Post Form " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }
    System.out.println(entity.getContentType());
 try{
  responsexml = entity.getContent();
 if(response!=null){
  System.out.println(entity.getContentLength());
 }

byte[] fileBArrayrawxml = new byte[(int)entity.getContentLength()]; responsexml.read(fileBArrayrawxml,0,(int)entity.getContentLength()); System.out.println(new String(fileBArrayrawxml)); responsexml.close(); }catch(Exception e){}finally{ responsexml.close(); } } }

harsh