- 浏览: 231948 次
文章分类
最新评论
-
iwangyiyx2:
...
加盐算法 -
seffxu:
我安装了run-jetty-run插件,并配置完成;通过Deb ...
jetty eclipse 插件 run-jetty-run
package test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class HttpURLConnectionTest { public static final String GET_URL = "http://localhost:8080/welcome1"; public static final String POST_URL = "http://localhost:8080/welcome1"; public static void readContentFromGet() throws IOException { // 拼凑get请求的URL字串,使用URLEncoder.encode对特殊和不可见字符进行编码 String getURL = GET_URL + "?username=" + URLEncoder.encode("fat man", "utf-8"); URL getUrl = new URL(getURL); // 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型, // 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection HttpURLConnection connection = (HttpURLConnection) getUrl .openConnection(); // 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到 // 服务器 connection.connect(); // 取得输入流,并使用Reader读取 BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); System.out.println("============================="); System.out.println("Contents of get request"); System.out.println("============================="); String lines; while ((lines = reader.readLine()) != null) { System.out.println(lines); } reader.close(); // 断开连接 connection.disconnect(); System.out.println("============================="); System.out.println("Contents of get request ends"); System.out.println("============================="); } public static void readContentFromPost() throws IOException { // Post请求的url,与get不同的是不需要带参数 URL postUrl = new URL(POST_URL); // 打开连接 HttpURLConnection connection = (HttpURLConnection) postUrl .openConnection(); // Output to the connection. Default is // false, set to true because post // method must write something to the // connection // 设置是否向connection输出,因为这个是post请求,参数要放在 // http正文内,因此需要设为true connection.setDoOutput(true); // Read from the connection. Default is true. connection.setDoInput(true); // Set the post method. Default is GET connection.setRequestMethod("POST"); // Post cannot use caches // Post 请求不能使用缓存 connection.setUseCaches(false); // This method takes effects to // every instances of this class. // URLConnection.setFollowRedirects是static函数,作用于所有的URLConnection对象。 // connection.setFollowRedirects(true); // This methods only // takes effacts to this // instance. // URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数 connection.setInstanceFollowRedirects(true); // Set the content type to urlencoded, // because we will write // some URL-encoded content to the // connection. Settings above must be set before connect! // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的 // 意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode // 进行编码 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成, // 要注意的是connection.getOutputStream会隐含的进行connect。 connection.connect(); DataOutputStream out = new DataOutputStream(connection .getOutputStream()); // The URL-encoded contend // 正文,正文内容其实跟get的URL中'?'后的参数字符串一致 String content = "firstname=" + URLEncoder.encode("一个大肥人", "utf-8"); // DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面 out.writeBytes(content); out.flush(); out.close(); // flush and close BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; System.out.println("============================="); System.out.println("Contents of post request"); System.out.println("============================="); while ((line = reader.readLine()) != null) { System.out.println(line); } System.out.println("============================="); System.out.println("Contents of post request ends"); System.out.println("============================="); reader.close(); connection.disconnect(); } /** * @param args */ public static void main(String[] args) { try { readContentFromGet(); readContentFromPost(); } catch (IOException e) { e.printStackTrace(); } } }
发表评论
-
Hudson问题汇总
2014-01-14 16:58 682混淆部分不识别嵌入class,在新文件中重新定义cl ... -
lenovo 后台访问方法
2013-07-22 10:23 0relayserver服务器也在249 ... -
lenovo日常记录(命令,脚本等)
2013-07-22 10:22 0mount -o remount,rw /syste ... -
AnyShare团队微分享——AR(增强现实)
2013-06-18 17:47 0问题: 1、什么是AR(图像增强)? 2、AR有哪 ... -
android AP介绍 和接口使用
2012-11-16 11:18 4399以下基于android ics系统 Android A ... -
一个变态的统计文件中log不重复字符的个数的程序
2012-11-15 00:17 958package com.xiaxing.io; i ... -
加盐算法
2012-10-23 11:04 10117附加(目前MD5,还有用RSA,速度稍慢,安全性更好) ... -
近距离传感器 (proximity sensor )
2012-10-19 17:02 27180近距离传感器的作用 ... -
java内存分析
2012-10-09 16:55 1014实时的工具有profilling/monitoring等,实时 ... -
代码心得
2012-08-14 14:03 812这次解决数据打包的问题,由于别人的代码错误导致程序关联性崩溃。 ... -
apache DataOutputStream 用法
2012-08-09 23:12 11class http { Htt ... -
appache read and write stream
2012-08-09 21:36 1024package com.cn.sevlet; impo ...
相关推荐
HttpURLConnectionDemo HttpURLConnectionDemo
web端就是一个简单的php页面,已经放在的我的sae albet.sinaapp.com上,直接使用demo中的url就行,当然,也可以把testPost.PHP放在自己的网站,把url改了就行
内含GET及POST方式连接服务器,并通过Json进行数据传输。 上一句是认真的,下面都是凑50字。
这个demo是Android使用HttpURLConnection上传图片到服务器
在 Android 中对 HttpURLConnection 的简单使用,仅供参考。
HttpURLConnection的demo,里面有网络请求get post 上传文件,下载文件,介绍HttpURLConnection这个类整体流程是怎样使用的,方便初学者学习
HttpURLConnection文件下载\httpURLConnection文件下载
Netroid框架 HttpURLConnection类 文件下载demo 主要目的是测量文件下载的速度,注意网络环境的一致性
HttpURLConnectionDemo.apk
自己写了一个基于httpurlconnection和Asynctask的例子
HttpUrlConnection获取json数据
//参数是a=""&b=""这样拼接的一个串 out.flush(); out.close(); out.write(parameters.getBytes(),0,parameters.getBytes().length); InputStream in = hc.getInputStream(); InputStreamReader ...
HttpURLConnection使用总结示例源码
代码包含了httpurlconnection基本操作,一些正常的操作。
使用HttpURLConnection发送短信
一个简单的例子,使用HttpURLConnection下载图片
java HttpURLConnection示例 HttpURLConnection工具
这是一个关于httpurlconnection 的网络post get 图片下载的Demo
封装httpurlconnection 自己动手丰衣足食
实例Demo程序来示范使用HttpURLConnection实现多线程下载。 使用多线程下载文件可以更快完成文件的下载,因为客户端启动多条线程进行下载就意味着服务器也需要为该客户端提供响应的服务。假设服务器同时最多服务100...