JAVA okhttp客户端和AWS服务器php脚本中的java.net.SocketTimeoutException

问题描述

我想从Android APP客户端使用Java中的okhttp发送一个表单主体,以检查服务器上是否有新文件,并通过AWS EC2服务器上的PHP脚本处理此请求,以将URL发送给新文件返回到APP。

客户端代码

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    static ResponseBody requestUpdate(Long name){
        Log.d(TAG,"requestUpdate: method starts");
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(60,TimeUnit.SECONDS)
                .readTimeout(60,TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS)
                .build();

        String fileName = Long.toString(name);
        RequestBody formBody = new FormBody.Builder()
                .add("name",fileName)
                .build();
        Request request = new Request.Builder()
                .url("https://url_to_aws_ec2_server/handleRequest.PHP")
                .post(formBody)
                .build();

        try (Response response = client.newCall(request).execute()) {
            Log.d(TAG,"requestUpdate: execute success");
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            System.out.println(response.body().string());
            return response.body();
        } catch (IOException e) {
            e.printstacktrace();
        }
        return null;
    }

服务器端代码

<?PHP
    header("Content-Type: application/json"); 

    $fileClient = pathinfo($_POST['name'],PATHINFO_FILENAME);
    $filedir = "./uploads/";

    // acquire the lastest file on server
    if(is_dir($filedir))
        {
            $fl=scandir($filedir);
            foreach($fl as $value){
                if($value !="." && $value!=".." && pathinfo($value,PATHINFO_EXTENSION) == "mp4"){
                    if(strcmp($value,$fileServer)>0){
                        $fileServer = $value;
                    }    
                }
            }
            // compare it with the one from client side
            if (strcmp($fileClient,$fileServer)>=0){
                $response['success'] = TRUE;
                $response['flag'] = 0;
            }else{
                $response['success'] = TRUE; 
                $response['flag'] = 1;
                $response['link'] = sprintf("http://url_to_ec2_server/%s",$fileServer);
            }
        }
        else{
            $response['success'] = FALSE;
            echo("Your directory does not exist.");
        }

    echo json_encode($response);
?>

我得到的错误

2020-08-25 15:01:28.574 2523-2579/com.example.ses_adplatform_test W/System.err: 
java.net.socketTimeoutException: 
Failed to connect to url_to_aws_ec2_server/13.59.157.98 (port 443) from /10.0.2.16 (port 54238) after 60000ms

我尝试过的事情

  1. 我在OkHttpClient构建器中将超时时间更改为2分钟-仍然会引发错误
  2. 我通过直接导航到PHP脚本来确保它可以正常工作-我在浏览器中得到了预期的响应
  3. 我已在AWS ec2实例上添加了HTTPS的入站和出站规则
  4. 我尝试将超时时间更改为5分钟,但是由于某种原因“运行时中止”,我的APP崩溃了。

我的问题是:

  1. 在这个特定问题上,服务器与我有关系吗?我是否应该以某种方式为okhttp客户端设置服务器?
  2. 还有其他我可以尝试解决的问题吗?还是你们认为可能会导致这种情况的任何可能原因?

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)