使用HttpURLConnection发送get和post请求

转载请注明出处:

我们在开发的使用,直接使用的开源框架,例如:Xutil,Volley开源框架直接访问网络,但是我们也需要知道其中的一些知识,,了解一下怎样访问网络的。下面我们模拟以下客户端和服务端,看看post和get请求。

首先我们开发一下客户端:

1.首先自定义线程,开启get请求。

public class GetThread extends Thread {private String name;private String age;private TextView show_content;private String url = "";private Handler handler = new Handler();public GetThread(String url, TextView show_content) {this.show_content = show_content;this.url = url;}public GetThread(String url, String name, String age, TextView show_content) {this.name = name;this.age = age;this.show_content = show_content;this.url = url;}@Overridepublic void run() {super.run();getRun();}private void getRun() {if (TextUtils.isEmpty(url)) {throw new NullPointerException("please ensure url is not equals null ");}BufferedReader bufferedReader = null;try {if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(age)) {url = url + "?name=" + URLEncoder.encode(name, "utf-8") + "&age=" + URLEncoder.encode(age, "utf-8");}URL httpUrl = new URL(url);HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection();httpURLConnection.setReadTimeout(5000);httpURLConnection.setRequestMethod("GET");//设置请求头headerhttpURLConnection.setRequestProperty("test-header","get-header-value");//获取内容InputStream inputStream = httpURLConnection.getInputStream();bufferedReader = new BufferedReader(new InputStreamReader(inputStream));final StringBuffer stringBuffer = new StringBuffer();String line = null;while ((line = bufferedReader.readLine()) != null) {stringBuffer.append(line);}handler.post(new Runnable() {@Overridepublic void run() {show_content.setText(stringBuffer.toString());}});} catch (Exception e) {} finally {if (bufferedReader != null) {try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}}}}}

对于Get请求,请求参数是拼接在Url上的。例如:?name=zhangsan&age=20

httpURLConnection.setReadTimeout(5000);设置超时时间

httpURLConnection.setRequestMethod("GET");设置请求方法

httpURLConnection.setRequestProperty("test-header","get-header-value");设置请求头header

获取从服务器传回来的内容

InputStream inputStream = httpURLConnection.getInputStream();

讲InputStream 转换成BufferedReader,便于操作流

将流中的数据存入到了StringBuffer中,最后设置给展示内容的TextView上。

最后要记得关闭流。

2.自定义PostThread线程,开启Post请求

public class PostThread extends Thread {private String name;private String age;private TextView show_content;private String url = "";private Handler handler = new Handler();public PostThread(String url, TextView show_content) {this.show_content = show_content;this.url = url;}public PostThread(String url, String name, String age, TextView show_content) {this.name = name;this.age = age;this.show_content = show_content;this.url = url;}@Overridepublic void run() {super.run();getRun();}private void getRun() {//Properties p=System.getProperties();//p.list(System.out);if (TextUtils.isEmpty(url)) {throw new NullPointerException("please ensure url is not equals null ");}BufferedReader bufferedReader = null;try {URL httpUrl = new URL(url);HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection();//设置请求头headerhttpURLConnection.setRequestProperty("test-header","post-header-value");httpURLConnection.setRequestMethod("POST");httpURLConnection.setReadTimeout(5000);//设置请求参数if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(age)) {OutputStream outputStream = httpURLConnection.getOutputStream();//String params="name="+ URLEncoder.encode(name, "utf-8")+"&age="+ URLEncoder.encode(age, "utf-8");String params="name="+ name+"&age="+ age;outputStream.write(params.getBytes());}//获取内容InputStream inputStream = httpURLConnection.getInputStream();bufferedReader = new BufferedReader(new InputStreamReader(inputStream));final StringBuffer stringBuffer = new StringBuffer();String line = null;while ((line = bufferedReader.readLine()) != null) {stringBuffer.append(line);}handler.post(new Runnable() {@Overridepublic void run() {show_content.setText(stringBuffer.toString());}});} catch (Exception e) {} finally {if (bufferedReader != null) {try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}}}}}

对于Post请求和Get请求有不同的地方

get请求是把请求参数拼接到Url中,可以在url中看到。而post请求不把请求参数放在了请求体中。

给Post设置请求参数

OutputStream outputStream = httpURLConnection.getOutputStream();获取请求连接的写入流

String params="name="+ name+"&age="+ age;拼接请求参数字符串

outputStream.write(params.getBytes());讲请求参数写入到写入流中

其他的地方和get请求是一样的。

每天告诉自己一次,我真的很不错

使用HttpURLConnection发送get和post请求

相关文章:

你感兴趣的文章:

标签云: