使用 Java 应用程序连接 PHP 脚本时出现问题

发布于 2024-10-31 11:45:11 字数 1406 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Bonjour°[大白 2024-11-07 11:45:11

您确定不是 PHP 脚本失败了吗?

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

只为守护你 2024-11-07 11:45:11

像这样对所有参数字符串进行编码:

String params = "part1" + "=" + part1;
params += "&" + "part2" + "=" + part2;
params = URLEncoder.encode(params, "UTF-8")

Encode all the params String like this:

String params = "part1" + "=" + part1;
params += "&" + "part2" + "=" + part2;
params = URLEncoder.encode(params, "UTF-8")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文