Thursday, July 28, 2016

HTTPURLConnection WebService Client

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.URL; import java.net.URLConnection; public class Test1 { private static final String serviceUrl = "http://www.webservicex.net/stockquote.asmx"; public static void main(String args[]) { try { final URL url = new URL(serviceUrl); StringBuilder message = new StringBuilder("<!--xml version=\"1.0\" encoding=\"UTF-8\"?-->"); message.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap= \"http://schemas.xmlsoap.org/soap/envelope/\">"); message.append(" <soap:Body>"); message.append(" <GetQuote xmlns=\"http://www.webserviceX.NET/\">"); message.append(" <symbol>").append("@").append("</symbol>"); message.append(" </GetQuote>"); message.append(" </soap:Body>"); message.append("</soap:Envelope>"); // Create the connection where we're going to send the file. URLConnection connection = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) connection; // Set the appropriate HTTP parameters. httpConn.setRequestProperty("Content-Length", String.valueOf(new String(message).length())); httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); httpConn.setRequestProperty("SOAPAction", "\"http://www.webserviceX.NET/GetQuote\""); httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); // Everything's set up; send the XML that was read in to the service. OutputStream out = httpConn.getOutputStream(); out.write(new String(message).getBytes()); out.close(); // Read the response and write it to standard out. InputStream inputStream = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(inputStream); BufferedReader in = new BufferedReader(isr); StringBuilder response = new StringBuilder(); String inputLine; while ((inputLine = in .readLine()) != null) { response.append(inputLine); } in .close(); System.out.println(response); } catch (Exception e) { e.printStackTrace(); } } } <!--xml version="1.0" encoding="utf-8"?--><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmln s:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetQuoteResponse xmlns="http://www.webserviceX.NET/"><GetQuoteResult>exception</GetQuoteResult></GetQuoteResponse ></soap:Body></soap:Envelope> //http://examples7x.wicket.apache.org/stock/wicket/bookmarkable/org.apache.wicket.examples.source.SourcesPage?0&SourcesPage_class=org.apache.wicket.examples.stockquote.StockQuotePage&source=StockQuotePage.html

No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...