Discussions

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("http://saintbook.org/MightyMeals/MightyMeals.asmx"); Properties sysProps = System.getProperties(); sysProps.put( "proxySet", "true" ); sysProps.put( "proxyHost", "proxy.myproxy.com"); 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: java.io.IOException: Server returned HTTP response code: 500 for URL: http://saintbook.org/MightyMeals/MightyMeals.asmxjava.io.IOException: Server returned HTTP response code: 500 for URL: http://saintbook.org/MightyMeals/MightyMeals.asmx at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1170) at Starter.startMeals(Starter.java:63) at Starter.main(Starter.java:28) 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 java.net 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", "\"http://www.Cookpedia.com/ForToday\""); uc.setRequestMethod("POST"); Good luck, Ivor