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. Win CE 5.0 增加电池电量显示

    摘自 http://blog.csdn.net/li0531/article/details/8818243 同时只在右上角显示一个 Label 控件,因此代码量精简了很多. [DllImport(& ...

  2. Linux 期中架构 Ansible

    ansible  自动化软件   基于Python开发 特点概述: 配置文件不需要过多配置  了解就可以了 ###部署ansble软件 ##受控主机部署 backup   nfs01   web01 ...

  3. 1077 Kuchiguse (20 分)

    1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  4. java.lang.Object类

    Object类是java所有对象的基类,包含着java最核心和最基础的类,在编译时会自动导入.具体的类结构图: 1.构造器 public Object(); 大部分情况下,Java中通过形如 new ...

  5. [UE4]C++ STL总结

    STL概述 STL (Standard Template Library, 标准模板库) 是惠普实验室开发的一系列软件的统称.主要核心分为三大部分:容器(container).算法(algorithm ...

  6. El中调用静态方法

    最近在项目中遇到需要调用静态方法的问题,形如: <c:forEach items="beans" var="bean"> <p>总数:$ ...

  7. BPM编程模型(场景)

    一直开发基于操作的业务系统,主要就是通过界面,用户提交一些数据完成任务,大多数涉及多人协作的,基本都是浏览,少数可能对其进行审批,这里的审批不是电子政务那样的多人审批任务,仅仅是对数据的一个操作而已, ...

  8. Spark 分布式SQL引擎

    SparkSQL作为分布式查询引擎:两种方式 SparkSQL作为分布式查询引擎:Thrift JDBC/ODBC服务 SparkSQL作为分布式查询引擎:Thrift JDBC/ODBC服务 Spa ...

  9. sweetalert插件使用

    内容: 1.插件介绍 2.插件使用 1.插件介绍 SweetAlert是一个JS插件,能够完美替代JS自带的alert弹出框,并且功能强大,设计优美 使用这个很方便,推荐使用这个插件来写alert s ...

  10. python3解析XML文件

    软硬件环境 Ubuntu 15.10 32bit Python 3.5.1 PyQt 5.5.1 前言 Python解析XML的方法挺多,本文主要是利用ElementTree来完成. 实例讲解 解析X ...