Reputation: 4547
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 );
}
Upvotes: 2
Views: 508
Reputation: 7480
Encode all the params String like this:
String params = "part1" + "=" + part1;
params += "&" + "part2" + "=" + part2;
params = URLEncoder.encode(params, "UTF-8")
Upvotes: -1