Java URL doesn't seem to connect, but no excep

2019-09-03 00:31发布

I have some code that needs to form a connection to a server, and once that connection is formed the server interprets the string that is sent and updates the names in a database. I.e. I send "http://app.sitename.tv/api/add/aName" and it adds "aName" to the database, or i send "http://app.sitename.tv/api/remove/aName" and it removes the name.

When I type these links into a browser and run it it works fine. From my program, though, it doesn't throw any exceptions but does not update the database. I'm not sure where to go from here. I've tried turning off my firewall but it makes no difference.

if ((Boolean) value)
{
    System.out.println("setValue");
    try 
    {
        String bvURL = "http://app.sitename.tv/api/add/" + data.name.get(row);
        URL myURL = new URL(bvURL);
        URLConnection myURLConnection = myURL.openConnection();
        myURLConnection.connect();
        System.out.println(bvURL);
    } 
    catch (MalformedURLException e) 
    { 
        System.err.println("MalformedURLException: " + e);
    } 
    catch (IOException e) 
    {   
        System.err.println("IOException: " + e);
    }
}
else if ((Boolean) value == false)
{
    System.out.println("setValue");
    try 
    {
        String bvURL = "http://app.sitename.tv/api/remove/" + data.name.get(row);
        URL myURL = new URL(bvURL);
        URLConnection myURLConnection = myURL.openConnection();
        myURLConnection.connect();
        System.out.println(bvURL);
    } 
    catch (MalformedURLException e) 
    { 
        System.err.println("MalformedURLException: " + e);
    } 
    catch (IOException e) 
    {   
        System.err.println("IOException: " + e);

    }
}

2条回答
狗以群分
2楼-- · 2019-09-03 00:44

The URLConnection you are getting is actually an HttpURLConnection. You have opened the connection but not closed it. To request that the HttpURLConnection be closed, you should call its disconnect() method. It could be that the connection needs to be closed in order to ensure that the server completely receives your request and processes it.

For example, you can replace your first try/catch block with this:

HttpURLConnection myURLConnection = null;
try {
    String bvURL = "http://app.sitename.tv/api/add/" + data.name.get(row);
    URL myURL = new URL(bvURL);
    myURLConnection = (HttpURLConnection) myURL.openConnection();
    myURLConnection.connect();
    System.out.println(bvURL);
} catch (MalformedURLException e) { 
    System.err.println("MalformedURLException: " + e);
} catch (IOException e) {   
    System.err.println("IOException: " + e);
} finally {
    if (myURLConnection != null) {
        myURLConnection.disconnect();
        myURLConnection = null;
    }
}

If this still does not work, you may need to get the InputStream from the connection, using getInputStream(), and consume all the input data before disconnecting.

查看更多
甜甜的少女心
3楼-- · 2019-09-03 00:51

If you don't check the response code or get the input or output streams, there is no connection at all. All you've done by calling connect() is create an HttpURLConnection object in your own JVM. Nothing more, and specifically nothing on the network whatsoever.

查看更多
登录 后发表回答