将URLConnection建立到另一个Web应用程序不适用于URLConnection .connect()? [英] Establishing the URLConnection to another web application does not work with URLConnection .connect()?

查看:194
本文介绍了将URLConnection建立到另一个Web应用程序不适用于URLConnection .connect()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我使用打开URLConnection到另一个webapllication的代码

Here is the code i am using the open the URLConnection to another webapllication

try {   
 URL   url = new URL("https://mySecondWebApp/Cart");
 HttpsURLConnection conn1 = (HttpsURLConnection) url.openConnection();//line1
 conn1 .connect();//line 2 does not establishes the connection
 conn1.getInputStream();// line 3 works which means that i get control to my app2 in debugger.But this also passes the control to IOException stating error java.io.FileNotFoundException: https://mySecondWebApp/Cart

 }
  catch (MalformedURLException e) {
    log.error("MalformedURLException" + e);

  }
  catch (IOException e) {

  log.info("getting IO error");
  }

我没有理解为什么第2行没有建立连接app2而第3行呢?
第二件事就是为什么我在第3行之后得到文件notfound异常,即使它成功连接到我想要避免的app2。我的意图
只是为了建立与app2的连接以便我得到控制里面我的java代码app2

推荐答案

不希望connect()方法从javadoc执行此任务:

The connect() method is not expected to do this task, from javadoc:

public abstract void connect()
                      throws IOException

如果尚未建立此类连接,则打开此URL引用的资源的通信链接。
如果在已经打开连接时调用connect方法(由连接字段指示值为true),则忽略该调用。
URLConnection对象经历两个阶段:首先创建它们,然后连接它们。在创建之后,在连接之前,可以指定各种选项(例如,doInput和UseCaches)。连接后,尝试设置它们是错误的。依赖于连接的操作(如getContentLength)将在必要时隐式执行连接。

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established. If the connect method is called when the connection has already been opened (indicated by the connected field having the value true), the call is ignored. URLConnection objects go through two phases: first they are created, then they are connected. After being created, and before being connected, various options can be specified (e.g., doInput and UseCaches). After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.

这篇关于将URLConnection建立到另一个Web应用程序不适用于URLConnection .connect()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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