HttpURLConnection

作者: Mayo酱 | 来源:发表于2017-09-26 14:25 被阅读0次

    1.获取HttpURLConnection实例,一般new出一个URL对象并且传入目标网络地址,再调用openConnection();

    URL url = new URL("http://localhost:8080/TestHttpURLConnectionPro/index.jsp"); 
    HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
    

    2.设置Get/Post

    connection.setRequestMethod("POST");  
    

    3.设置链接超时、读取超时毫秒数,消息头等

    connection.setConnectTimeout(8000);
    connection.setReadTimeout(8000);
    

    4.getInputStream()获取服务器返回的输入流

    InputStream in = connection.getInputStream();
    

    5.关闭HTTP链接

    connection.disConnect();
    

    相关文章

      网友评论

        本文标题:HttpURLConnection

        本文链接:https://www.haomeiwen.com/subject/vrpsextx.html