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(二)的更多相关文章

  1. android-数据存储之远程服务器存储

    一.如何编码实现客户端与服务器端的交互 <一>JDK内置原生API HttpUrlConnection <二>Android内置的包装API HttpClient浏览器 < ...

  2. Android入门(二十)HttpURLConnection与HttpClient

    原文链接:http://www.orlion.ga/679/ 在 Android上发送 HTTP请求的方式一般有两种,HttpURLConnection和 HttpClient. 一.HttpURLC ...

  3. 【JAVA】通过HttpURLConnection 上传和下载文件(二)

    HttpURLConnection文件上传 HttpURLConnection采用模拟浏览器上传的数据格式,上传给服务器 上传代码如下: package com.util; import java.i ...

  4. 【转】 Pro Android学习笔记(七二):HTTP服务(6):HttpURLConnection

    目录(?)[-] Http Get的使用方式 基础小例子 Cookie的使用 重定向 HTTP POST的小例子 基础小例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载 ...

  5. 通过HttpURLConnection 上传和下载文件(二)

    HttpURLConnection文件上传 HttpURLConnection采用模拟浏览器上传的数据格式,上传给服务器 上传代码如下: package com.util; import java.i ...

  6. RTX二次开发集成

    1,rtx服务器端有很多端口,二次发的程序与这些打开的端口交互.打开端口的方法在rtx服务管理器中,默认http服务未启用.需要手动启用http端口如下: 如果打开rtx服务器没有启用http的801 ...

  7. android 之HttpURLConnection的post,get方式请求数据

    get方式和post方式的区别: 1.请求的URL地址不同: post:"http://xx:8081//servlet/LoginServlet" get:http://xxx: ...

  8. java生成带logo的二维码,自定义大小,logo路径取服务器端

    package com.qishunet.eaehweb.util; import java.awt.BasicStroke; import java.awt.Graphics; import jav ...

  9. HttpURLConnection 文件上传限制

    一.      问题 最近在Android程序里上传向.Net服务器上传大文件的时候,发现了一个问题.当上传大文件的时候会爆出OutOfMemoryError,上传小文件则没有这种情况. 二.     ...

随机推荐

  1. shell 2变量

    shell变量 定义变量 变量名命名规则: 1.命名只能使用英文字母.数字和下划线,首个字符不能以数字开头 2.中间不能有空格,可以使用下划线 3.不能使用标点符号 4.不能使用sh中的关键字,可用h ...

  2. Python 天气预报+微信

    """ Description: 需要提供以下三个信息,在申请到的微信企业号当中可以找到 agentid corpid corpsecret Author:Nod Dat ...

  3. 搭建OpenStack先电云平台

    实际操作示意图 在VMware里面创建两台centos7的虚拟机作为搭建云平台的两节点配置如下: 1.第一台虚拟机   作为控制节点 2CPU 3G以上内存 硬盘50G 网络适配器一个nat 一个仅主 ...

  4. TSubobjectPtr和C++传统指针的区别

    转自:http://aigo.iteye.com/blog/2282142 主要有以下区别(1和2的前提条件要满足:指针所在的class必须是UObjcct的子类): 1,TSubobjectPtr指 ...

  5. 普适注意力:用于机器翻译的2D卷积神经网络,显著优于编码器-解码器架构

    现有的当前最佳机器翻译系统都是基于编码器-解码器架构的,二者都有注意力机制,但现有的注意力机制建模能力有限.本文提出了一种替代方法,这种方法依赖于跨越两个序列的单个 2D 卷积神经网络.该网络的每一层 ...

  6. .NET MVC ToList() 转Json

    #region 方法一 #region ToList()转json /// <summary> /// 通过类别 Id 获相应产品 /// </summary> /// < ...

  7. 2017-2018-2 20165233 实验四 Android程序设计

    20165233 实验四 Android程序设计 实验内容 任务一: 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECANDROID,安装 Andr ...

  8. PY3 周总结 第一周

    1. Python的三大特点是什么? 答:解释型.动态类型(运行期间才做数据类型检查).强类型定义(一个变量只能存储一种类型的数据).[See More] 2. 应该使用Python2 还是 Pyth ...

  9. WPF 颜色拾色器

    效果图: 下载:Code 参考: http://www.codeproject.com/Articles/33001/WPF-A-Simple-Color-Picker-With-Previewhtt ...

  10. 浅谈RMI的特点及作用

    RMI:远程方法调用(Remote Method Invocation) 扩展:RPC与RMI的区别 1:方法调用方式不同: RMI中是通过在客户端的Stub对象作为远程接口进行远程方法的调用.每个远 ...