• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 655
  • Last Modified:

java new URL(). Exception when url is outside of local network.

This method works fine if the url is within local network (from multiple computers) but gives
java.lang.ArrayIndexOutOfBoundsException: 1
if remote url is specified.

How can this be fixed?

Thank you.
try{
        
        // construct data
        String data = URLEncoder.encode("UserName", "UTF-8") + "=" + URLEncoder.encode(userName, "UTF-8");
        data += "&" + URLEncoder.encode("Password", "UTF-8") + "=" + URLEncoder.encode(userPassword, "UTF-8");
        data += "&" + URLEncoder.encode("MAC", "UTF-8") + "=" + URLEncoder.encode(macAddress, "UTF-8");
        //data += "&" + URLEncoder.encode("browser", "UTF-8") + "=" + URLEncoder.encode(this.browserName, "UTF-8");
        
        // Send data
        //URL url = new URL("http://localhost/RecordInstallations.php");
        URL url = new URL("http://tmthv2.byethost13.com/RecordInstallations.php");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        
        conn.setUseCaches(false);
        conn.setInstanceFollowRedirects(false);
        conn.setAllowUserInteraction(false);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
    
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            processServerMessage(line);
        }
        wr.close();
        rd.close();
      } catch (Exception e){
          System.out.print(e);
      }

Open in new window

0
AlexanderR
Asked:
AlexanderR
  • 8
  • 7
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
AlexanderR,

Hello again.

Try breaking it apart. http://java.sun.com/j2se/1.4.2/docs/api/java/net/URL.html

URL url = new URL(String protocol, String host, int port, String file)

Also check the spelling of the hostname, should it be bytehost13?
try {
    URL url = new URL("http", "tmthv2.byethost13.com", 80, "RecordInstallations.php");
} catch (MalformedURLException e) {
    // ... handle error ...
}

Open in new window

0
 
AlexanderRAuthor Commented:
Promissing suggestion but no go.

But i found something interesting.
URL url = new URL("http", "tmthv2.byethost13.com", 80, "RecordInstallations.php");
gives
java.io.IOException: Server returned HTTP response code: 400 for URL: http://tmthv2.byethost13.com:80RecordInstallations.php

So its communicating!

That url is obviously wrong so i changed it to
URL url = new URL("http", "tmthv2.byethost13.com", 80, "/RecordInstallations.php");
and got
java.lang.ArrayIndexOutOfBoundsException: 1

Does that mean anything?
0
 
AlexanderRAuthor Commented:
Actually no, its not communicating.  Thats just error 400 because of invalid URL structure.  If i give it a non existing file name it gives me outofbounds thing.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Kevin CrossChief Technology OfficerCommented:
Interesting?  Do you have a proxy?
0
 
AlexanderRAuthor Commented:
No, just a usual linksys  router.
0
 
Kevin CrossChief Technology OfficerCommented:
Hmmm.

I just tried from my machine and it worked (except I didn't have any data to post) so just posted nothing.  I was going to suggest using conn.connect() but I actually got it working with or without that.

Here is the response I got as I changed the processServerMessage line of code to:
System.out.println(line);
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://byet.org/web/index.html">here</a>.</p>
</body></html>

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Through a debug on, as I just tested posting dummy data, so the message you are getting is either because of a connection problem out to internet or during the actual posting of real data and/or the processServerMessage routing.  Debug will tell you exactly where unless you are already get a line number in code from error message.
0
 
AlexanderRAuthor Commented:
Now, thats an interesting response...
So there is something wrong with the host then??
Perhaps they are somehow redirecting the response along the way.  I can go to the file i need through the browser but, as you noted, java gives me that html page and i have no clue where its comming from or why its there instead of my page.

I guess i will have to look into that.
0
 
Kevin CrossChief Technology OfficerCommented:
I know on previous question you add these extra settings to get the postings to work in Opera (I think IIRC), but try to change this -- I got curious about trying a GET instead of POST as I got answer from web page in my browser too that says didn't recognize computer and to call you. :) Then looked at this line that thought was curious given the message was about redirecting page:

conn.setInstanceFollowRedirects(false);
change to true and get further in process.
0
 
AlexanderRAuthor Commented:
After chaning it to true, it got a page with load of javascript on it which, i assume, the browser must process in order to go on to the page.  Obviously this java applet isn't going to be doing that.

Can try using text-based browser (lynx or links) to see whats going on

So this host is based on a bunch of complex redirects all over the place.  Thats a real problem...
Then there is nothing wrong with the Java Applet, and having it work with this host is a completly different question.  I guess i have to ask in their support forums if there is any way around it.
0
 
Kevin CrossChief Technology OfficerCommented:
Sounds like it.  Sorry could be more of a help, but maybe that at least saves you losing sleep over code that is actually working fine.
0
 
AlexanderRAuthor Commented:
Yes.  Your comments saved me lots of trouble.  Unlike in PHP, i am so new to Java that i still dont know where to begin looking yet.
I'll accept comment with conn.setInstanceFollowRedirects(true);
since it showed where the problem is.

Thanks a lot!
0
 
AlexanderRAuthor Commented:
I would not be able to isolate the problem by myself.
0
 
Kevin CrossChief Technology OfficerCommented:
No problem, you are welcome.

Btw, PHP 5+ (maybe lower too) can run Java classes directly, so if there are some functions you could do in PHP to this site, but some functionality you need in java like for a business object or the piece of code grabbing the MAC addresss, you can just create the java object to do its stuff but have other parts of your code in PHP.

Just some more food for thought.
0
 
Kevin CrossChief Technology OfficerCommented:
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now