我HTTPURLConnection用来连接到Web服务.我知道如何使用,HTTPURLConnection但我想了解它是如何工作的.基本上,我想知道以下内容:
HTTPURLConnection尝试建立与给定URL的连接?getOutputStream和getInputStream外行的任期?我注意到,当我试图连接到服务器宕机,我得到Exception的getOutputStream.这是否意味着HTTPURLConnection我只会在调用时开始建立连接getOutputStream?怎么样getInputStream?因为我只能得到响应getInputStream,那么这是否意味着我还没有发送任何请求getOutputStream但只是建立连接?HttpURLConnection我调用时是否回到服务器请求响应getInputStream?openConnection只是创建一个新的连接对象,但尚未建立任何连接?java inputstream outputstream urlconnection httpurlconnection
如何使用Java在一个HttpURLConnection中执行多个请求?
URL url = new URL("http://my.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
HttpURLConnection.setFollowRedirects( true );
connection.setDoOutput( true );
connection.setRequestMethod("GET");
PrintStream ps = new PrintStream( connection.getOutputStream() );
ps.print(params);
ps.close();
connection.connect();
//TODO: do next request with other url, but in same connection
Run Code Online (Sandbox Code Playgroud)
谢谢.