HttpURLConnection(二)
package com.cmy.urlcon; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection; public class HttpURLConnectionDemo { public static void main(String[] args) throws Exception {
// crwyUrl();
//getURLData1();
//getURLData2(); urlSupportType();
} public static void crwyUrl() throws Exception { URL url = new URL(
"http://blog.csdn.net/a9529lty/article/details/6454145");
Object obj = url.getContent();
System.out.println(url.getContent());
System.out.println(obj.getClass().getName());
HttpURLConnection con = (HttpURLConnection) url.openConnection(); Reader read = new InputStreamReader(url.openStream());
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection.getInputStream();
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
url.getAuthority(); } public static void getURLData1() throws Exception {
URL url = new URL(
"http://lavasoft.blog.51cto.com/attachment/200811/200811271227767778082.jpg");
Object obj = url.getContent();
System.out.println("getURLData1获取指定资源: " + obj.getClass().getName());
} public static void getURLData2() throws Exception {
URL url = new URL(
"http://blog.csdn.net/chenzheng_java/article/details/6248090");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
in.close();
} public static void getURLData3() throws Exception {
URL url = new URL(
"http://blog.csdn.net/chenzheng_java/article/details/6248090");
InputStream in = url.openStream();
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
in.close();
} public static void urlSupportType() throws Exception {
String host = "www.java2s.com";
String file = "/index.html"; String[] schemes = { "http", "https", "ftp", "mailto", "telnet",
"file", "ldap", "gopher", "jdbc", "rmi", "jndi", "jar", "doc",
"netdoc", "nfs", "verbatim", "finger", "daytime",
"systemresource" }; for (int i = 0; i < schemes.length; i++) {
try {
URL u = new URL(schemes[i], host, file);
System.out.println(schemes + " is supported\r\n");
} catch (Exception ex) {
System.out.println(schemes + " is not supported\r\n");
}
}
} /**
* HttpURLConnection getMethod
* @return
* @throws Exception
*/
public static String urlDoGet() throws Exception{
URL url = new URL("http://localhost:8080/OneHttpServer/");
HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setRequestProperty("Accept-Charset","utf-8");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setConnectTimeout(3000);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
String cookie0 = con.getHeaderField("Set-Cookie"); InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bfReader = null;
StringBuffer sb = new StringBuffer();
String readLine = null; if(con.getResponseCode() >= 300){
throw new Exception("HTTP Request is not success, Response code is " + con.getResponseCode());
} try {
inputStream = con.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
bfReader = new BufferedReader(inputStreamReader);
while((readLine = bfReader.readLine()) != null){
sb.append(readLine);
} } catch (Exception e) {
e.printStackTrace();
}finally{
bfReader.close();
inputStreamReader.close();
inputStream.close();
} return sb.toString();
} /**
* HttpURLConnection postMethod
* @return
* @throws Exception
*/
public static String urlDoPost() throws Exception{
URL url = new URL("http://localhost:8080/OneHttpServer/");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
String parameterData = "username=nickhuang&blog=http://www.cnblogs.com/nick-huang/"; con.setRequestProperty("Accept-Charset","utf-8");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setConnectTimeout(3000);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
String cookie0 = con.getHeaderField("Set-Cookie"); OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bfReader = null;
StringBuffer sb = new StringBuffer();
String readLine = null; try {
outputStream = con.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(parameterData.toString());
outputStreamWriter.flush(); } catch (Exception e) {
// TODO: handle exception
} if(con.getResponseCode() >= 300){
throw new Exception("HTTP Request is not success, Response code is " + con.getResponseCode());
} try {
inputStream = con.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
bfReader = new BufferedReader(inputStreamReader);
while((readLine = bfReader.readLine()) != null){
sb.append(readLine);
} } catch (Exception e) {
e.printStackTrace();
}finally{
outputStreamWriter.close();
outputStream.close();
bfReader.close();
inputStreamReader.close();
inputStream.close();
} return sb.toString();
}
}
HttpURLConnection(二)的更多相关文章
- android-数据存储之远程服务器存储
一.如何编码实现客户端与服务器端的交互 <一>JDK内置原生API HttpUrlConnection <二>Android内置的包装API HttpClient浏览器 < ...
- Android入门(二十)HttpURLConnection与HttpClient
原文链接:http://www.orlion.ga/679/ 在 Android上发送 HTTP请求的方式一般有两种,HttpURLConnection和 HttpClient. 一.HttpURLC ...
- 【JAVA】通过HttpURLConnection 上传和下载文件(二)
HttpURLConnection文件上传 HttpURLConnection采用模拟浏览器上传的数据格式,上传给服务器 上传代码如下: package com.util; import java.i ...
- 【转】 Pro Android学习笔记(七二):HTTP服务(6):HttpURLConnection
目录(?)[-] Http Get的使用方式 基础小例子 Cookie的使用 重定向 HTTP POST的小例子 基础小例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载 ...
- 通过HttpURLConnection 上传和下载文件(二)
HttpURLConnection文件上传 HttpURLConnection采用模拟浏览器上传的数据格式,上传给服务器 上传代码如下: package com.util; import java.i ...
- RTX二次开发集成
1,rtx服务器端有很多端口,二次发的程序与这些打开的端口交互.打开端口的方法在rtx服务管理器中,默认http服务未启用.需要手动启用http端口如下: 如果打开rtx服务器没有启用http的801 ...
- android 之HttpURLConnection的post,get方式请求数据
get方式和post方式的区别: 1.请求的URL地址不同: post:"http://xx:8081//servlet/LoginServlet" get:http://xxx: ...
- java生成带logo的二维码,自定义大小,logo路径取服务器端
package com.qishunet.eaehweb.util; import java.awt.BasicStroke; import java.awt.Graphics; import jav ...
- HttpURLConnection 文件上传限制
一. 问题 最近在Android程序里上传向.Net服务器上传大文件的时候,发现了一个问题.当上传大文件的时候会爆出OutOfMemoryError,上传小文件则没有这种情况. 二. ...
随机推荐
- 杂项:IIS
ylbtech-杂项:IIS IIS是Internet Information Services的缩写,意为互联网信息服务,是由微软公司提供的基于运行Microsoft Windows的互联网基本服务 ...
- python3+selenium3.13的简单操作
1.浏览器 1.1 浏览器窗口大小位置 driver.set_window_size(self, width, height, windowHandle) 将某个窗口设置为固定大小 driver.se ...
- ROS的工作模式和ESXI网卡工作模式的关系
1.ROS网卡如果工作在桥接模式,那么ESXI网卡的工作模式必须设置为Promiscuous Mode(混杂模式)和Forged Transmits(伪传输)这两个必须都为开启状态,如下: 这种情况, ...
- java.lang.Object类
Object类是java所有对象的基类,包含着java最核心和最基础的类,在编译时会自动导入.具体的类结构图: 1.构造器 public Object(); 大部分情况下,Java中通过形如 new ...
- sklearn.svm.SVC 参数说明
原文地址:sklearn.svm.SVC 参数说明 ============================== 资源: sklearn官网+DOC 库下载GitHub =============== ...
- 全国城市空气质量实时数据(PM2.5)实时下载
国家公布了“http://113.108.142.147:20035/emcpublish/”空气质量实时发布平台,WCF服务地址为“http://113.108.142.147:20035/EnvP ...
- Openstack kvm win7镜像制作
本文地址http://www.cnblogs.com/tcicy/p/7790956.html 网上找了很多为openstack制作win7镜像的文章,总是不成功 自己写一下,以便大家查看. 我使用c ...
- 使用 Nmon 监控 Linux 的系统性能
Nmon(得名于 Nigel 的监控器)是IBM的员工 Nigel Griffiths 为 AIX 和 Linux 系统开发的一款计算机性能系统监控工具.Nmon 可以把操作系统的统计数据展示在屏幕上 ...
- 最近玩的centos7.3用的防火墙转载
CentOS 7 firewalld使用简介 1.firewalld简介 firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务:第二个就是加入了防火墙的“zone ...
- robot framework取出列表子元素
取出嵌套列表变量的子元素 ${list}型列表: ${list} = [["A1", "first"], ["A2", "seco ...