使用 Java 应用程序连接 PHP 脚本时出现问题
我有一个 swing 应用程序,当用户单击其中的 JButton 时,它会连接到我网站中的 php 脚本,向其发送一些数据并从 PHP 脚本检索结果。
这对于使用该应用程序的数百名用户来说效果很好,但今天一家公司的一位用户报告说他无法使用该应用程序......当他单击按钮时,应用程序挂起并且没有任何反应。
我什至使用 UncaughtExceptionHandler 来处理应用程序中的任何意外异常,但没有抛出任何异常。我认为这可能是他公司网络的问题,或者使用的端口的问题,但我不确定。 对于为什么会发生这种情况有什么建议吗?
这是我的代码:
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 );
}
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 );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定不是 PHP 脚本失败了吗?
Are you sure it's not the PHP script that's failing?
像这样对所有参数字符串进行编码:
Encode all the params String like this: