最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
在Java中可以使用HttpURLConnection发起这两种请求,了解此类,对于了解soap,和编写servlet的自动测试代码都有很大的帮助。
下面的代码简单描述了如何使用HttpURLConnection发起这两种请求,以及传递参数的方法:
public class HttpInvoker {
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) {
// TODO Auto-generated method stub
try {
readContentFromGet();
readContentFromPost();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
转自:http://toughpatient5.diandian.com/post/2011-07-06/19241336
http://java.chinaitlab.com/net/831820.html
http://dusiguxia.blog.163.com/blog/static/5562931620119193430501/
http://androidyo.iteye.com/blog/624015
相关推荐
NULL 博文链接:https://zjutsoft.iteye.com/blog/1313754
HttpURLConnection的(get请求、post请求)
HttpURLConnection的get,post和图片加载
里面详细的写了post和get和HttpPost和HttpGet请求
基于HttpURLConnection的封装http请求,进行了https校验忽略,对get和post进行了整合,更方便请求,对header以及超时的进行了可配置化处理,对返回也进行了封装,便于请求结果的处理。
这是一个关于httpurlconnection 的网络post get 图片下载的Demo
小编刚刚用HttpURLConnection做了一个android的APP,不小心踩到了几个坑,总结下最常用的就通过HttpURLConnection来POST提交JSON数据与GET请求JSON数据。此外就是下载图片,下载图片分为显示进度与不显示进度两种。...
httpurlconnection网络请求,get请求, post请求,封装
//使用POST提交 hc.addRequestProperty("Cookie", getSessionId()); hc.setDoOutput(true); hc.connect(); OutputStream out = hc.getOutputStream(); //参数是a=""&b=""这样拼接的一个串 out....
HttpURLConnection的demo,里面有网络请求get post 上传文件,下载文件,介绍HttpURLConnection这个类整体流程是怎样使用的,方便初学者学习
Android中网络请求一般使用Apache HTTP Client或者采用HttpURLConnection,但是直接使用这两个类库需要写大量的代码才能完成网络post和get请求,而使用这个MyHttpUtils库可以大大的简化操作,它是基于...
android请求web服务器的四种方式 HttpURLConnection 方式下的get post请求、 HttpGet请求 HttpPost请求
一些工具类代码块的标准代码: 异步的Get请求 异步的Post请求 Get请求,获得返回数据 向指定 URL 发送POST方法的请求 使用finally块来关闭输出流和输入流
用于异步执行 HttpGet、HttpPost、HttpPut、HttpDelete、Multipart-Put、Multipart-Post 和带有回调的 HttpUrlConnection 的库。 简化网络通信所需的代码量。 兼容 Android 2.2 及以上版本。 特征 兼容Android 2.2 ...
首先需要明确的是,Http通信中的POST和GET请求方式的不同。GET可以获得静态页面,也可以把参数放在URL字符串后面,传递给服务器。而POST方法的参数是放在Http请求中。因此,在编程之前,应当首先明确使用的请求...
封装了HttpURLConnection,便于http的post和get请求,可以进行http的文件上传和下载,https免验证请求,请求日志
HTTP协议是一款基于短连接的协议,每次交互完毕后连接断开,而HTTP请求又分为GET和POST两种方式,GET请求比较简单,只需要在网址后面用?拼接请求的资源路径,如百度图片输入动漫关键字的地址 ht
用GET还是POST 提交数据用POST 打开输入和输出流 获取上传信息 字节大小以及长度 设置请求体的类型是文本类型 获得输出流 向服务器输出数据 获得服务器响应的结果和状态码 如果 返回码等于200 得到服务器返回的输入...
使用Java的HttpURLConnection类连接到服务器,并发送GET请求获取要下载的文件的基本信息,包括文件名、文件大小等。 2.2 分块下载 将要下载的文件分成多个块,每个块使用一个线程进行下载。每个线程需要记录已下载...
Java中可以使用HttpURLConnection或者Apache HttpClient等工具类进行文件下载,通过设置请求头和请求参数,发送get或post请求获取文件。 使用Java压缩多个文件为zip包 Java中可以使用ZipOutputStream类将多个文件...