关于HttpURLConnection请求发送和数据接收 1.创建HttpURLConnection连接URL url = new URL("http://localhost:8080/xxx.do"); URLConnection rulConnection = url.openConnection();// 此处的urlConnection对象实际上是根据URL的
1.创建HttpURLConnection连接 URL url = new URL("http://localhost:8080/xxx.do"); URLConnection rulConnection = url.openConnection();// 此处的urlConnection对象实际上是根据URL的 // 请求协议(此处是http)生成的URLConnection类 // 的子类HttpURLConnection,故此处最好将其转化 // 为HttpURLConnection类型的对象,以便用到 // HttpURLConnection更多的API.如下: HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection; 2.设置HttpURLConnection参数 // 设定请求的方法为"POST",默认是GET httpUrlConnection.setRequestMethod("POST"); // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在 // http正文内,因此需要设为true, 默认情况下是false; httpUrlConnection.setDoOutput(true); // 设置是否从httpUrlConnection读入,默认情况下是true; httpUrlConnection.setDoInput(true); // Post 请求不能使用缓存 httpUrlConnection.setUseCaches(false); // 设定传送的内容类型是可序列化的java对象 // (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException) httpUrlConnection.setRequestProperty("Content-type", "application/x-java-serialized-object"); // 连接,从上述url.openConnection()至此的配置必须要在connect之前完成, httpUrlConnection.connect(); 3.URLConnection建立连接 // 此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法, // 所以在开发中不调用上述的connect()也可以)。 OutputStream outStrm = httpUrlConnection.getOutputStream(); 4.HttpURLConnection发送请求 // 现在通过输出流对象构建对象输出流对象,以实现输出可序列化的对象。 ObjectOutputStream objOutputStrm = new ObjectOutputStream(outStrm); // 向对象输出流写出数据,这些数据将存到内存缓冲区中 objOutputStrm.writeObject(new String("我是测试数据")); // 刷新对象输出流,将任何字节都写入潜在的流中(些处为ObjectOutputStream) objOutputStm.flush(); // 关闭流对象。此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中, // 在调用下边的getInputStream()函数时才把准备好的http请求正式发送到服务器 objOutputStm.close(); 5.获取HttpURLConnection请求响应 // 调用HttpURLConnection连接对象的getInputStream()函数, InputStream inStrm = httpConn.getInputStream(); **************以下代码是自己的实际应用代码片段************************** long startTime = System.currentTimeMillis(); //请求起始时间_毫秒 //STO签名 String timestamp = String.valueOf(System.currentTimeMillis()); //请求起始时间_毫秒 //${basepath}表示接口部署路劲,生产环境调用过程中换成 http://vip.sto.cn/ signature.append(info.get("url")); signature.append("${basepath}/PreviewInterfaceAction.action"); signature.append("?code=").append(info.get("rmcode")); //请求的方法名 signature.append("&data_digest=").append(info.get("data_digest")); //方法签名 signature.append("&cuspwd=").append(info.get("cuspwd")); //客户密码 signature.append("&cusname=").append(info.get("cusname")); //客户名称 signature.append("&cusite=").append(info.get("cusite")); //网点名称 signature.append("&len=").append(info.get("expressmaxcnt")); //个数 String signatures = signature.toString(); URL url = new URL(info.get("url")); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-Type","application/Json; charset=UTF-8"); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("post"); connection.connect(); // OutputStream outStrm = connection.getOutputStream(); // ObjectOutputStream objOutputStrm = new ObjectOutputStream(outStrm); // objOutputStrm.flush(); //清空缓冲区,发送数据 // objOutputStrm.close(); InputStream inStrm = connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); String temp = ""; String str=""; while((temp = in.readLine()) != null){ str = str + temp; } System.out.println("输出返回数据:"); System.out.println("lalala"+str); long endTime = System.currentTimeMillis();请求和响应流程.jpg