URL.openConnection()和URLConnection.connect()之间的区别? [英] Difference between URL.openConnection() and 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 atconnection.connect();
?如何在
connection
对象中设置URL并将其与connection.connect()
一起使用?(因为我对.openConnection()
不太满意)How can I set URL in the
connection
Object and use it withconnection.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屋!