Solved

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

Posted on 2008-10-28
15
640 Views
Last Modified: 2013-12-29
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
Comment
Question by:AlexanderR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22824988
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
 
LVL 11

Author Comment

by:AlexanderR
ID: 22825114
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
 
LVL 11

Author Comment

by:AlexanderR
ID: 22825203
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22825398
Interesting?  Do you have a proxy?
0
 
LVL 11

Author Comment

by:AlexanderR
ID: 22825428
No, just a usual linksys  router.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22826660
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22826691
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
 
LVL 11

Author Comment

by:AlexanderR
ID: 22826839
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
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22826945
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
 
LVL 11

Author Comment

by:AlexanderR
ID: 22827028
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22827203
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
 
LVL 11

Author Comment

by:AlexanderR
ID: 22827224
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
 
LVL 11

Author Closing Comment

by:AlexanderR
ID: 31510890
I would not be able to isolate the problem by myself.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22827238
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22827244
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question