Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why ami getting java.net.UnknownHostException ?

843789Jul 16 2009 — edited Jul 16 2009
This is my requirement.
URL->
 https://test.webservices.test.com    
Module Version to hit Request XML: 
http://webservices.test.com/1ASIWPOC1A/ABCD_06_1_1A?;

Request XML:
<Security_Authenticate><user_id>XXXXX</user_id><password>XXXXXX</password> </Security_Authenticate>
This is my code
package webservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.jdom.Element;



import com.sun.xml.internal.txw2.Document;

public class TestWebService {
	public static void main(String[] args) {
		String sRequestXML="<Security_Authenticate><user_id>XXXXX</user_id><password>XXXXXX</password> </Security_Authenticate>";
		sendAndReceiveXML(sRequestXML);
	}
private static String sendAndReceiveXML(String xmlRequest) {
		
		StringBuffer sbResponse = new StringBuffer();
		URL url = null;
		boolean debugWebSvc=true;
		
		try {
			if (debugWebSvc) 
			{
				System.out.println("Request is ------------------: ");
				System.out.println(xmlRequest);
			}

			url = new URL("http://webservices.test.com/1ASIWPOC1A/ABCD_06_1_1A");
			URLConnection conn = url.openConnection();
			conn.setRequestProperty("Content-Type", "text/xml");

			conn.setDoOutput(true);
			OutputStreamWriter wr=null;
			try {
				wr = new OutputStreamWriter(conn.getOutputStream());
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			wr.write(xmlRequest);
			wr.flush();

			// Get the response
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			System.out.println("IN HERE");
			while ((line = rd.readLine()) != null) {
				
				sbResponse.append(line);
			}
			wr.close();
			rd.close();

			if (true) {
				System.out.println("Response is : ");
				System.out.println(sbResponse);
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
			sbResponse.setLength(0);
			sbResponse.append(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			sbResponse.setLength(0);
			sbResponse.append(e.getMessage());
		}

		return sbResponse.toString();

	}
}

O/P :
Request is ------------------: 
<Security_Authenticate><user_id>XXXXX</user_id><password>XXXXXX</password> </Security_Authenticate>
java.net.UnknownHostException: webservices.test.com
	at java.net.PlainSocketImpl.connect(Unknown Source)
	at java.net.SocksSocketImpl.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at sun.net.NetworkClient.doConnect(Unknown Source)
	at sun.net.www.http.HttpClient.openServer(Unknown Source)
	at sun.net.www.http.HttpClient.openServer(Unknown Source)
	at sun.net.www.http.HttpClient.<init>(Unknown Source)
	at sun.net.www.http.HttpClient.New(Unknown Source)
	at sun.net.www.http.HttpClient.New(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
	at webservice.TestWebService.sendAndReceiveXML(TestWebService.java:42)
	at webservice.TestWebService.main(TestWebService.java:20)
Why am i getting this exception ? I am totally new to webservices.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 13 2009
Added on Jul 16 2009
13 comments
328 views