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,上传小文件则没有这种情况. 二. ...
随机推荐
- ThinkPHP框架学习摘要
框架在linux与win下区别 1.文件权限设置: 2.大小写不规范: 学习框架的基本思路 : 1.如何收入并配置框架: 2.Controller的命名规范与书写规范: 3.Model的命名规范与书写 ...
- python redis启用线程池管理
pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT,max_connections=3,password=REDIS_PASSWO ...
- eclipse 常用jar包总结
BeanUtils: DbUtils: FileUpload: IO: Lang: Logging: cglib: mysql-connector: Pool:[datasource] DBCP:[d ...
- 【Linux_Unix系统编程】Chapter9 进程凭证
chapter9 进程凭证 每个进程都有一套用数字表示的用户ID(UID)和组ID(GID).有时也将这些ID称子为进程凭证. 1:实际用户ID和实际组ID 2:有效用户ID和有效组ID 3:保存的s ...
- Spark JDBC入门测试
spark jdbc分支源码下载地址 https://github.com/apache/spark/tree/branch-1.0-jdbc 编译spark jdbc ./make-distrib ...
- ESB初步配置文件认识
每个项目的都有各自的场景,但是其实往小处说,场景的处理基本都是很相似,之前做copy文件的程序,其实就是一种很常见的ETL的过程(转移文件,异构系统通过文件系统交换数据,存在数据同步). 了解一下ET ...
- php 流程控制switch实例
switch允许对一个标量(表达式)的多个可能结果做选择. 语法: switch (expr) { case result1: statement1 break; case result2: stat ...
- Glusterfs3.3.1DHT(hash分布)源代码分析
https://my.oschina.net/uvwxyz/blog/182224 1.DHT简介 GlusterFS使用算法进行数据定位,集群中的任何服务器和客户端只需根据路径和文件名就可以对数据进 ...
- 装饰模式 (Decoratory)
动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更灵活. 装饰模式就是利用 SetComponent 来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个 ...
- hive xml udf
<store> <book id="book"><title id="titile">hive</title> ...