-
Web access on non-dialup
Alright, I'm designing something that needs to have internet access. The program works wonderfully on dialup, but go to any form of internet that accesses it's information over a LAN and it dies from a connection error. Is there something I'm not doing clientside on IE/Mozilla? can Java access the internet over a LAN? Has someone already designed something that allows for non-dialup web transfer in Java? and if so, where is it?
Thanks for your help!
-
huh? there's no difference. Java just uses TCP/IP, it doesnt care what the link layer transport is.. you need to give more information on what youre actually doing, because at the moment it sounds like your lan card or settings ont he networked computer are incorrect
-
*does a basic test* wow, I feel stupid... it seems to just be a problem with the HTTPClient class then....
Basically, I'm writing a java gui front for a PHPChat and this is the class I'm using for the information transfer... it's never worked on any form of network I've been on... However, it worked perfectly on dialup
Code:
import java.util.*;
import java.io.*;
import java.net.*;
public class HttpClient
{
protected URL url;
protected HttpURLConnection server;
/**
* @param szUrl: String object for the URL
*/
public HttpClient(String szUrl) throws Exception
{
try
{
url = new URL(szUrl);
}
catch (Exception e)
{
throw new Exception("Invalid URL");
}
}
/**
* @param method: String object for client method (POST, GET,...)
*/
public void connect(String method) throws Exception
{
try
{
server = (HttpURLConnection)url.openConnection();
server.setDoInput(true);
server.setDoOutput(true);
server.setRequestMethod(method);
server.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
server.connect();
}
catch (Exception e)
{
throw new Exception("Connection failed");
}
}
public void disconnect()
{
server.disconnect();
}
public String displayResponse() throws Exception
{
String line;
String return_="";
try
{
BufferedReader s = new BufferedReader(
new InputStreamReader(
server.getInputStream()));
line = s.readLine();
while (line != null)
{
return_+=line+"\n";
//System.out.println(line);
line = s.readLine();
}
s.close();
}
catch(Exception e)
{
throw new Exception("Unable to read input stream");
}
return return_;
}
public void post(String s) throws Exception
{
try
{
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
server.getOutputStream()));
bw.write(s, 0, s.length());
bw.flush();
bw.close();
}
catch(Exception e)
{
throw new Exception("Unable to write to output stream");
}
}
public static void main(String argv[])
{
if (argv.length == 0)
{
System.out.println("Usage: java HttpClient url\r\n");
System.exit(0);
}
try
{
HttpClient c = new HttpClient(argv[0]);
c.connect("GET");
c.displayResponse();
c.disconnect();
c.connect("POST");
c.post("data=Posted request");
c.displayResponse();
c.disconnect();
c.connect("POST");
c.post("data=2nd request");
c.displayResponse();
c.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
and if you're wondering, yes, that was taken off the internet.... although I can't exactly remember the source
-
It's not a problem from my lan, though your code has some curious design quirks:
Code:
import java.util.*;
import java.io.*;
import java.net.*;
public class HttpClient
{
protected URL url;
protected HttpURLConnection server;
/**
* @param szUrl: String object for the URL
*/
public HttpClient(String szUrl) throws Exception
{
try
{
url = new URL(szUrl);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* @param method: String object for client method (POST, GET,...)
*/
public void connect(String method) throws Exception
{
try
{
server = (HttpURLConnection)url.openConnection();
server.setDoInput(true);
server.setDoOutput(true);
server.setRequestMethod(method);
//server.setRequestProperty("Content-type",
// "application/x-www-form-urlencoded");
server.connect();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void disconnect()
{
server.disconnect();
}
public String displayResponse() throws Exception
{
String line;
String ret="";
try
{
BufferedReader s = new BufferedReader(
new InputStreamReader(
server.getInputStream()));
line = s.readLine();
while (line != null)
{
ret+=line+"\n";
//System.out.println(line);
line = s.readLine();
}
s.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return ret;
}
public void post(String s) throws Exception
{
try
{
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
server.getOutputStream()));
bw.write(s, 0, s.length());
bw.flush();
bw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String argv[])
{
if (argv.length == 0)
{
System.out.println("Usage: java HttpClient url\r\n");
System.exit(0);
}
try
{
HttpClient c = new HttpClient(argv[0]);
System.out.println("Getting");
c.connect("GET");
c.displayResponse();
c.disconnect();
System.out.println("Posting1");
c.connect("POST");
c.post("data=Posted request");
c.displayResponse();
c.disconnect();
System.out.println("Posting2");
c.connect("POST");
c.post("data=2nd request");
c.displayResponse();
c.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
You catch an exception then throw it again? This is nigh on pointless.. if you do not wish to do naything when an error occurs, do not catch it. if you wish to handle it there and then, in 100% completeness, then handle it.. Do not catch then throw, unless you wish to change the type of exception.. e.g. catch IOException, throw new SukuriException. There are only specific cases where this is necessary..
Additionally, your error messages that you placed into the new exceptions, were not much help.. they were very generic and any failure was reported as "error reading stream" when actually a far more accurate response of "HTTP 501 - Method Not Implemented" was returned..
try to avoid C++isms like:
String return_=""
in java, this looks ugly; we arent used to looking at nasty variable names with trailing underscores..
You werent very verbose in your test harness.. i ran the app against microsoft.com for a while, and nothing happened.. i added some printlns and found it to be working as expected, but with no output :)
the Content-Type header may not be specified as part of a request, only as part of a response. If you send it, the server will ignore it. For a full list of HTTP/1.1 request headers that your request may send, see page 38 of RFC2616: ftp://ftp.isi.edu/in-notes/rfc2616.txt
-
I believe I said I took that from the internet and was just using it. I find it quite humorous that it was working partially... I'll look over it again and re-write all the confusing and ignorant catch/throws statements. Maybe I should have done this in the first place
-
Originally posted by sukuri
I believe I said I took that from the internet and was just using it..
where? 
ps; if youre going to get really into this, dump that junk code and use HTTPUnit
-
Originally posted by sukuri
and if you're wondering, yes, that was taken off the internet.... although I can't exactly remember the source
^-- there, under all the code, but yeah.
I can assume HTTPUnit is the real, professional version of what was being written with all the frills expected from something designed to do everything?
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