|
-
Use servlet directly from application/applet (post/get)
Hi,
I am trying to run a servlet directly from an application (or an Applet if
you want). Nothing could be more simple I thought, but I always get a
FileNotFoundException. However it does seem to work whenever I try to use
this address in my webbroser (I get the proper output of the servlet), and
also it seems to work when I try to connect an outside server serving a
servlet. I found many pointers to this problem out on the web, but none with
a solution for it (there are even references on the Java Bug Parade).
If this does not work, then this could mean a serious problem for whoever
wants to make a B2B integration solution, as this is mostly the work he
needs to do.
// case 1:
URL url = new URL("http","127.0.0.1",80,"/asn/servlet/SnoopServlet");
// case 2:
// URL url = new
URL("http","www.somerealserveroutthere.com",80,"/asn/servlet/SnoopServlet");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoInput(true);
http.setDoOutput(true);
http.setUseCaches(false);
http.setRequestProperty("Content-Type", "text/plain");
PrintWriter writer = new PrintWriter(http.getOutputStream());
writer.println("Do Some Output");
writer.flush();
writer.close();
String inputLine;
// Gives FileNotFoundException (except case 2):
BufferedReader in = new BufferedReader(new
InputStreamReader(http.getInputStream()));
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
//Also Would give FileNotFoundException if previous commented out:
int ret = http.getResponseCode();
if (ret != HttpURLConnection.HTTP_OK) {
System.err.println("Could Not Post ASN's");
}
http.disconnect();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks