相关疑难解决方法(0)

在Java中什么时候URL连接关闭?

什么时候java放弃了到URL的连接?我没有在URL或URLConnection上看到close()方法,所以它会在请求完成后立即释放连接吗?我主要是想在异常处理程序中查看是否需要进行任何清理.

try {
  URL url = new URL("http://foo.bar");
  URLConnection conn = url.openConnection();
  // use the connection
}
catch (Exception e) {
  // any clean up here?
}
Run Code Online (Sandbox Code Playgroud)

java connection url exception

74
推荐指数
2
解决办法
6万
查看次数

HttpURLConnection实现

我已经读过HttpURLConnection支持持久连接,因此可以为多个请求重用连接.我尝试了它,发送第二个POST的唯一方法是第二次调用openConnection.否则我得到一个IllegalStateException("已经连接"); 我使用了以下内容:

try{
URL url = new URL("http://someconection.com");
}
catch(Exception e){}
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//set output, input etc
//send POST
//Receive response
//Read whole response
//close input stream
con.disconnect();//have also tested commenting this out
con = (HttpURLConnection) url.openConnection();
//Send new POST
Run Code Online (Sandbox Code Playgroud)

第二个请求是通过相同的TCP连接发送的(用wireshark验证它)但我无法理解为什么(虽然这是我想要的)因为我已经调用了disconnect.我检查了HttpURLConnection的源代码,并且实现确实保持了对相同目标的连接的keepalive缓存.我的问题是,在发送第一个请求后,我无法看到连接如何放回缓存中.断开连接关闭连接,没有断开连接,我仍然无法看到连接如何放回缓存.我看到缓存有一个run方法来遍历所有空闲连接(我不确定它是如何被调用的),但我找不到连接如何放回缓存中.似乎唯一发生的地方是httpClient的完成方法,但是没有调用带响应的POST.谁可以帮我这个事?

编辑 我的兴趣是,对于tcp连接重用,HttpUrlConnection对象的正确处理是什么.应该关闭输入/输出流,然后是url.openConnection(); 每次发送新请求(避免disconnect())?如果是,我第二次调用url.openConnection()时无法看到连接是如何重用的,因为第一个请求已从缓存中删除了连接,但无法找到返回的连接方式.是否有可能连接没有返回到keepalive缓存(bug?),但操作系统尚未发布tcp连接,在新连接上,OS返回缓冲连接(尚未发布)或类似的东西? EDIT2 我找到的唯一相关内容来自JDK_KeepAlive

...当应用程序在URLConnection.getInputStream()返回的InputStream上调用close()时,JDK的HTTP协议处理程序将尝试清理连接,如果成功,则将连接放入连接缓存以供将来的HTTP请求重用.

但我不确定这是哪个处理程序.sun.net.www.protocol.http.Handler没有做任何缓存,因为我看到谢谢!

java network-programming jdk1.6 httpurlconnection

17
推荐指数
2
解决办法
3万
查看次数

在iOs和浏览器上API调用比在android上快得多

HttpsConnection在Android上遇到了麻烦.

首先,不,它不是重复.我在SO上尝试了几乎所有的解决方案,比如更改keep-alive选项或者timeout(其中一些确实优化了我的代码的一部分)但是它在Android上比在iOS上慢5到10倍(可能更多).

在Android上发送请求到我的服务器需要几秒钟,而在iOS和浏览器上它几乎是即时的.我确信服务器没有原因.但似乎获得输入流非常慢!

这一行:

in=conn.getInputStream();
Run Code Online (Sandbox Code Playgroud)

是最拖延的,它本身需要几秒钟.

我的目标是从我的服务器获取JSON.我的代码应该在技术上尽可能地优化(并且它可能可以帮助一些人HttpsConnection在同一时间):

protected String getContentUrl(String apiURL)
    {
        StringBuilder builder = new StringBuilder();
        String line=null;
        String result="";
        HttpsURLConnection conn= null;
        InputStream in= null;
        try {
            URL url;
            // get URL content
            url = new URL(apiURL);
            System.setProperty("http.keepAlive", "false");
            trustAllHosts();
            conn = (HttpsURLConnection) url.openConnection();
            conn.setHostnameVerifier(DO_NOT_VERIFY);
            conn.setRequestMethod("GET");
            conn.setRequestProperty(MainActivity.API_TOKEN, MainActivity.ENCRYPTED_TOKEN);
            conn.setRequestProperty("Connection", "close");
            conn.setConnectTimeout(1000);

            in=conn.getInputStream();


            // open the stream and put it into BufferedReader
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            while ((line=br.readLine())!= …
Run Code Online (Sandbox Code Playgroud)

android json httpconnection

5
推荐指数
0
解决办法
515
查看次数

HttpURLConnection PHP 脚本未获取数据

我尝试了许多不同的方法来尝试使用 HttpURLConnection 将数据从我的 Android 应用程序上传到我服务器上的 PHP 脚本,但在服务器上由 PHP 创建的文件中没有显示任何内容。我使用 HTTPClient 取得了成功,但我不得不切换到使用 HttpURLConnection。应用程序在运行时不会崩溃。我确信我忽略了一些简单的东西。我的 PHP 脚本运行良好,甚至返回了预期的响应,但我的 Android 代码有一些我还没有看到的错误。任何帮助表示赞赏。

这是 PHP 脚本的开头:

  $data = $_POST["deviceSIG"];
Run Code Online (Sandbox Code Playgroud)

这是我用来将数据上传到 PHP 脚本的代码:

// the string deviceSIG is defined elsewhere and has been defined in the class.

private class MyAsyncTask extends AsyncTask<String, Integer, String>{
            @Override
            protected String doInBackground(String... params)           
     try{
                URL url = new URL("http://192.168.10.199/user_script.php");

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.connect();             

                OutputStream outputStream = conn.getOutputStream();
                OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
                writer.write(deviceSIG);
                writer.close(); …
Run Code Online (Sandbox Code Playgroud)

php android httpurlconnection

0
推荐指数
1
解决办法
3222
查看次数