URL.openConnection()和URLConnection.connect()之间的区别? [英] Difference between URL.openConnection() and URLConnection.connect()?

查看:233
本文介绍了URL.openConnection()和URLConnection.connect()之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中:

HttpURLConnection connection = (HttpURLConnection)createMerchURL.openConnection();
connection.setRequestMethod("PUT");
connection.addRequestProperty("Name", "Value1");

connection.connect();

..
connection.disconnect();

  • 实际上什么时候打开连接?在..createMerchURL.openConnection();处?还是在connection.connect();?

    • When is the connection getting opened actually? At ..createMerchURL.openConnection(); ? or at connection.connect();?

      如何在connection对象中设置URL并将其与connection.connect()一起使用?(因为我对.openConnection()不太满意)

      How can I set URL in the connection Object and use it with connection.connect()?(as I am less comfortable with .openConnection())

      最后,两者之间有什么区别吗?

      Finally, is there any difference between the two?

      谢谢..

      推荐答案

      实际上什么时候打开连接?在..createMerchURL.openConnection(); ?或在connection.connect();处?

      When is the connection getting opened actually? At ..createMerchURL.openConnection(); ? or at connection.connect();?

      后者.前者只是解析URL,找到协议,然后创建HttpURLConnection对象.

      The latter. The former just parses the URL, finds the protocol, and creates the HttpURLConnection object.

      如何在连接对象中设置URL

      How can I set URL in the connection Object

      你不能.

      并与connection.connect()一起使用?

      and use it with connection.connect()?

      你不能.

      (因为我不太喜欢.openConnection())

      (as I am less comfortable with .openConnection())

      运气不好:对此感到满意.

      Bad luck: get comfy with it.

      这篇关于URL.openConnection()和URLConnection.connect()之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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