Access to web service with HttpURLConnection.


XML & Web services: Access to web service with HttpURLConnection.

  1. Access to web service with HttpURLConnection. (1 messages)

    Hi. I Have problem with this code which suppose to call web service operation through proxy server. private static void startMeals() { String message = "" +" "; HttpURLConnection uc = null; URL url = null; byte[] data = message.getBytes(); try { url = new URL(""); Properties sysProps = System.getProperties(); sysProps.put( "proxySet", "true" ); sysProps.put( "proxyHost", ""); sysProps.put( "proxyPort", "3128" ); } catch (MalformedURLException e1) { System.err.println("URL EXCEPTION"); e1.printStackTrace(); } try { uc = (HttpURLConnection) url.openConnection(); uc.setDoInput(true); uc.setDoOutput(true); uc.setUseCaches(false); uc.setRequestProperty("Content-Length", " " + message.length()); uc.connect(); DataOutputStream dos = new DataOutputStream(uc.getOutputStream()); dos.write(data, 0, data.length); dos.flush(); dos.close(); BufferedReader br = new BufferedReader(new InputStreamReader(uc .getInputStream())); String res = null; while ((res = br.readLine()) != null) System.out.println(res); br.close(); } catch (IOException e) { System.err.println("From client: " + e); e.printStackTrace(System.out); uc.disconnect(); System.err.println("Disconnected..."); } } And have following error: From client: Server returned HTTP response code: 500 for URL: Server returned HTTP response code: 500 for URL: at at Starter.startMeals( at Starter.main( What could be wrong?
  2. The request should be SOAP compliant[ Go to top ]

    Dear Alex, Your request doesn't look SOAP compliant to me. You should do a POST instead of a default GET, provide a SOAPAction request header and set the Content-Type. I would recommend using httpclient (or an other modern, flexible and consistent library) instead of the classes for bare http-requests. You can then easily switch on wire-debugging to see the exchanged messages... Or you could use a webservice-stub library/framework to deal with the SOAP low-level stuff... In the meantime, the solution to get your sample code working is to insert before "uc.connect()": uc.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); uc.setRequestProperty("SOAPAction", "\"\""); uc.setRequestMethod("POST"); Good luck, Ivor