HttpURLConnection 用法详解



一.继承关系

   1.  java.lang.Object

          --java.net.URLConnection

               --java.net.HttpURLConnection

二.URLConnection类

  1.URLConnection代表应用程序和 URL 之间的通信链接。

  2.作用:代表应用程序和 URL 之间的通信链接;

  3. 创建一个到 URL 的连接需要几个步骤:

   (1)通过在 URL 上调用 openConnection 方法创建连接对象;

   (2)处理设置参数和一般请求属性;

   (3)使用 connect 方法建立到远程对象的实际连接;

   (4)远程对象变为可用。远程对象的头字段和内容变为可访问;

三.HttpURLConnection

  1 每个 HttpURLConnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络。请求后在 HttpURLConnection 的 InputStream 或 OutputStream 上调用 close() 方法可以释放与此实例关联的网络资源,但对共享的持久连接没有任何影响。如果在调用 disconnect() 时持久连接空闲,则可能关闭基础套接字;

  2. 对于Connection的对象的设置很重要,是有顺序的;

     connection的设置(很多set函数)必须在函数执行之前完成;Outputstream的写操作必须在Inputstream读操作之前。

四.用法

     1.创建连接:

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

     HttpURLConnection conn = ( HttpURLConnection) url.openConnection();

  2.设置Connection参数:

     conn.setRequestMethod( "POST");        // 提交模式

     conn.setRequestProperty( "Content-Type", "application/json;charset=UTF-8" );               //设置请求属性

     conn.setConnectTimeout(100000); //连接超时 单位毫秒

     conn.setReadTimeout(100000);    //读取超时 单位毫秒

     conn.setDoOutput( true);         //是否输入参数

     conn.setDoInput( true);        //是否读取参数

    3.连接:

     conn.connect();

   4.获取写数据流:

     OutputStream outStrm = httpUrlConnection.getOutputStream();

   5.写数据:

     outStrm.write(bytes); // 输入参数

     outStrm. flush();

     outStrm.close();

   6.读数据:

     InputStream in = conn.getInputStream();

     int count=conn.getContentLength();//获取远程资源长度

     byte[] buffer = new byte[count];

     ByteArrayOutputStream baos = new ByteArrayOutputStream();

     for ( int len = 0; (len = in.read(buffer)) > 0;) {

          baos.write(buffer, 0, len);

     }

     String returnValue = new String(baos.toByteArray(), "utf-8" );

     reg= JSON. parseObject(returnValue, ReturnMessage.class );

     baos.flush();

     baos.close();

     in.close();

     conn.disconnect();

 

 

HttpURLConnection 用法详解

一.继承关系

1. java.lang.Object

--java.net.URLConnection

--java.net.HttpURLConnection

二.URLConnection类

1.URLConnection代表应用程序和 URL 之间的通信链接。

2.作用:代表应用程序和 URL 之间的通信链接;

3. 创建一个到 URL 的连接需要几个步骤:

(1)通过在 URL 上调用 openConnection 方法创建连接对象;

(2)处理设置参数和一般请求属性;

(3)使用 connect 方法建立到远程对象的实际连接;

(4)远程对象变为可用。远程对象的头字段和内容变为可访问;

三.HttpURLConnection

1 每个 HttpURLConnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络。请求后在 HttpURLConnection 的 InputStream 或 OutputStream 上调用 close() 方法可以释放与此实例关联的网络资源,但对共享的持久连接没有任何影响。如果在调用 disconnect() 时持久连接空闲,则可能关闭基础套接字;

2. 对于Connection的对象的设置很重要,是有顺序的;

connection的设置(很多set函数)必须在函数执行之前完成;Outputstream的写操作必须在Inputstream读操作之前。

四.用法

1.创建连接:

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

HttpURLConnection conn = ( HttpURLConnection) url.openConnection();

2.设置Connection参数:

conn.setRequestMethod( "POST"); // 提交模式

conn.setRequestProperty( "Content-Type", "application/json;charset=UTF-8" ); //设置请求属性

conn.setConnectTimeout(100000); //连接超时 单位毫秒

conn.setReadTimeout(100000); //读取超时 单位毫秒

conn.setDoOutput( true); //是否输入参数

conn.setDoInput( true); //是否读取参数

3.连接:

conn.connect();

4.获取写数据流:

OutputStream outStrm = httpUrlConnection.getOutputStream();

5.写数据:

outStrm.write(bytes); // 输入参数

outStrm. flush();

outStrm.close();

6.读数据:

InputStream in = conn.getInputStream();

int count=conn.getContentLength();//获取远程资源长度

byte[] buffer = newbyte[count];

ByteArrayOutputStream baos = new ByteArrayOutputStream();

for ( int len = 0; (len = in.read(buffer)) > 0;) {

baos.write(buffer, 0, len);

}

String returnValue = new String(baos.toByteArray(), "utf-8" );

reg= JSON. parseObject(returnValue, ReturnMessage.class );

baos.flush();

baos.close();

in.close();

conn.disconnect();

 

posted @ 2019-12-18 14:32  憨憨青年  阅读(2678)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css