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,上传小文件则没有这种情况. 二. ...
随机推荐
- 杂项-frame:Rails框架
ylbtech-杂项-frame:Rails框架 Rails框架首次提出是在2004年7月,它的研发者是26岁的丹麦人David Heinemeier Hansson.不同于已有复杂的Web 开发框架 ...
- Splunk 交流
1. 初识splunk Splunk Enterprise Splunk Free Splunk Universal Forwarder,通用转发器
- 学习MongoDB 一:MongoDB 入门(安装与配置)
一.简介 MongoDB一种非关系型数据库(NoSql),是一种强大.灵活.可扩展的数据存储方式,因为MongoDB是文档模型,自由灵活很高,可以让你在开发过程中畅顺无比,对于大数据量.高并发.弱事务 ...
- python-log-env
logging.basicConfig(format="[%(asctime)s] %(filename)s[line:%(lineno)d] %(levelname)s: %(messag ...
- MPI 环境配置,MPICH,VisualStudio
▶ Visual Studio 下配置MPI环境 ● 参考资料:http://blog.csdn.net/z909768094/article/details/50926162 ● 如果使用 MPIC ...
- 转发 DDoS攻防战 (一) : 概述
岁寒 然后知松柏之后凋也 岁寒 然后知松柏之后凋也 ——论语·子罕 (此图摘自<Web脚本攻击与防御技术核心剖析>一书,作者:郝永清先生) DDoS,即 Distributed ...
- chrome浏览器控制台 console不打印信息问题解决办法。
转自:https://blog.csdn.net/wang17866603359/article/details/79083776 最近换了安装chrome,想按F12调试下代码,发现控制台什么信息都 ...
- 解决org.springframework.context.NoSuchMessageException: No message found under code 'login.validate.er
转自:https://blog.csdn.net/steveguoshao/article/details/36184971 在项目中遇到 org.springframework.context.No ...
- as2 无法加载类或接口
1.最大问题就是可能新建文件的时候选择as3的as或者直接复制了as3的as文件过来修改.as文件必须是2的 2.类名是否一致 3.包引用是否正确
- state介绍
state是salt最核心的功能,通过预先定制好的sls(salt state file)文件对被控主机进行状态管理,支持包括程序包(pkg).文件(file).网络配置(network).系统服务( ...