Java URL似乎没有连接,但没有抛出异常 [英] Java URL doesn't seem to connect, but no exception thrown

查看:295
本文介绍了Java URL似乎没有连接,但没有抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码需要与服务器建立连接,一旦形成连接,服务器就会解释发送的字符串并更新数据库中的名称。即我发送 http://app.sitename.tv/api/add/aName 它将aName添加到数据库中,或者我发送
http:// app .sitename.tv / api / remove / aName 并删除名称。

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);

    }
}


推荐答案

如果您不检查响应代码或获取输入或输出流,则根本没有连接。您通过调用connect()完成的所有工作就是在您自己的JVM中创建一个HttpURLConnection对象。没什么,特别是在网络上没有任何东西。

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.

这篇关于Java URL似乎没有连接,但没有抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆