2

I have a swing application that when the user clicks on a JButton in it, it connects to a php script in my website to send some data to it and retrieve the results from the PHP script.

This worked fine for 100s of users who used this application, but today one of the users in a company reported that he cannot use this ... When he clicks the button the application hangs and nothing happens.

I even use UncaughtExceptionHandler to handle any unexpected exceptions in the application, but nothing is thrown. I thought it may be something in his company's network, or the port used, but i am not sure. Any suggestions why this may happen ?

Here is my code :

String part1 = "...";  // Message part 1.
String part2 = "...";  // Message part 2.

//1. Encode the message to suite the URL path requirements :
    String params = URLEncoder.encode( "part1", "UTF-8" ) + "=" + URLEncoder.encode( part1, "UTF-8" );
    params += "&" + URLEncoder.encode( "part2", "UTF-8" ) + "=" + URLEncoder.encode( part2, "UTF-8" );

//2. Connect to the website page :
    URL url = new URL( "http://www.website.com/page.php" );
    URLConnection conn = (URLConnection) url.openConnection();
    conn.setConnectTimeout( 20000 );
    conn.setDoOutput( true );
    conn.setDoInput( true );
    conn.connect();

//3. Call the page and send the parameters to it :
    OutputStreamWriter out = new OutputStreamWriter( conn.getOutputStream() ); 
    out.write( params );
    out.flush();
    out.close();

//4. Get the result :
    Object contents = conn.getContent();
    InputStream is = (InputStream) contents;
    StringBuffer buf = new StringBuffer();
    int c;
    while( ( c = is.read() ) != -1 ) {
      buf.append( (char) c );
    }
8
  • Can he load that URL in his web browser? Commented Apr 7, 2011 at 20:22
  • If I were you, I'd use commons-httpclient Commented Apr 7, 2011 at 20:27
  • I'm not sure why you explicitly mentioned "a company" in the question. Is it within some corporate network with a restrictive proxy/firewall? Is it the same company as the other 100s users are member of? Commented Apr 7, 2011 at 20:47
  • @CanSpice: I can send the user the URL to try it, but what will this differ ? What are you thinking of ? Commented Apr 7, 2011 at 21:27
  • @BalusC: I have mentioned that because most of my users are individuals, they use the program on their personal PCs(Every user is from a different city or country), but this user is in a company which is not the normal case, they have network department controlling their access, and his admin already asked which TCPIP ports are being used during the script request. I am not so good in networking, but what i understand i am using the default port 80. Commented Apr 7, 2011 at 21:33

2 Answers 2

2

Are you sure it's not the PHP script that's failing?

1
  • I suppose you could have made this a comment :) ... Yes, i am sure. The script works fine for all other users + The program has other scripts to connect to my server like checking program updates, or reporting a problem from the program. The user tried all those, and none worked ... All hangs. Commented Apr 7, 2011 at 21:12
-1

Encode all the params String like this:

String params = "part1" + "=" + part1;
params += "&" + "part2" + "=" + part2;
params = URLEncoder.encode(params, "UTF-8")
3
  • What does that have to do with his problem? Commented Apr 7, 2011 at 20:35
  • 2
    He's already encoding them the right way. You should not encode = and &. Otherwise it becomes one large parameter name. Commented Apr 7, 2011 at 20:39
  • Thanks. I didn't know that, I always use commons-httpclient. Commented Apr 7, 2011 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.