android笔记之--向网络发送GET/POST请求参数
2011-09-15 10:59
639 查看
使用[b]GET[/b]方法发送请求
使用[b]POST[/b]方法发送请求
我们先从IE浏览器中使用POST方法发送一次:(下面内容可以用HttpWatch看到)
POST /videoService/login HTTP/1.1
Accept: image/jpeg,
application/x-ms-application, image/gif, application/xaml+xml,
image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, application/QVOD,
application/QVOD, */*
Referer: http://192.168.100.91:8080/videoService/login.jsp
Accept-Language: zh-CN,en;q=0.5
User-Agent:
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB6.5;
SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media
Center PC 6.0; Tablet PC 2.0)
Content-Type: application/x-www-form-urlencoded//POST请求这个一定要设置
Accept-Encoding: gzip, deflate
Host: 192.168.100.91:8080
Content-Length: 26//还有发送内容长度也要设置
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=7E1435CB8A071D07A430453250348C41
username=asd&password=1234//这里是请求体部分,一共26个字节,与Content-Length长度一样
以Android集成的HttpClient框架来发送
private static boolean sendGETRequest (String path, Map<String, String> params) throws Exception{ //发送地http://192.168.100.91:8080/videoService/login?username=abc&password=123 // StringBuilder是用来组拼请求地址和参数 StringBuilder sb = new StringBuilder(); sb.append(path).append("?"); if(params!=null &¶ms.size()!=0){ for (Map.Entry<String, String> entry : params.entrySet()) { //如果请求参数中有中文,需要进行URLEncoder编码 sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8")); sb.append("&"); } sb.deleteCharAt(sb.length()-1); } URL url = new URL(sb.toString()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode()==200){ return true; } return false; }
使用[b]POST[/b]方法发送请求
我们先从IE浏览器中使用POST方法发送一次:(下面内容可以用HttpWatch看到)
POST /videoService/login HTTP/1.1
Accept: image/jpeg,
application/x-ms-application, image/gif, application/xaml+xml,
image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, application/QVOD,
application/QVOD, */*
Referer: http://192.168.100.91:8080/videoService/login.jsp
Accept-Language: zh-CN,en;q=0.5
User-Agent:
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB6.5;
SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media
Center PC 6.0; Tablet PC 2.0)
Content-Type: application/x-www-form-urlencoded//POST请求这个一定要设置
Accept-Encoding: gzip, deflate
Host: 192.168.100.91:8080
Content-Length: 26//还有发送内容长度也要设置
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=7E1435CB8A071D07A430453250348C41
username=asd&password=1234//这里是请求体部分,一共26个字节,与Content-Length长度一样
private static boolean sendPOSTRequest(String path, Map<String, String> params) throws Exception{ // StringBuilder是用来组拼请求参数 StringBuilder sb = new StringBuilder(); if(params!=null &¶ms.size()!=0){ for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8")); sb.append("&"); } sb.deleteCharAt(sb.length()-1); } // entity为请求体部分内容 //如果有中文则以UTF-8编码为username=%E4%B8%AD%E5%9B%BD&password=123 byte[] entity = sb.toString().getBytes(); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); //要向外输出数据,要设置这个 conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", entity.length+""); OutputStream os = conn.getOutputStream(); //以POST方式发送请求体 os.write(entity); if(conn.getResponseCode()==200){ return true; } return false; }
以Android集成的HttpClient框架来发送
private static boolean sendPOSTRequestHttpClient(String path, Map<String, String> params) throws Exception{ //封装请求参数 List<NameValuePair> pair = new ArrayList<NameValuePair>(); if(params!=null&& !params.isEmpty()){ for(Map.Entry<String, String> entry:params.entrySet()){ pair.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } //把请求参数变成请求体部分 UrlEncodedFormEntity uee = new UrlEncodedFormEntity(pair, "utf-8"); //使用HttpPost对象设置发送的URL路径 HttpPost post = new HttpPost(path); //发送请求体 post.setEntity(uee); //创建一个浏览器对象,以把POST对象向服务器发送,并返回响应消息 DefaultHttpClient dhc = new DefaultHttpClient(); HttpResponse response = dhc.execute(post); if(response.getStatusLine().getStatusCode()==200){ Log.i("http", "httpclient"); return true; } return false; }
相关文章推荐
- android笔记之--向网络发送GET/POST请求参数
- android笔记之--向网络发送GET/POST请求参数
- 向网络发送GET/POST请求参数
- Android使用HttpURLConnection发送网络请求(笔记)
- android网络通讯之HttpClient4不指定参数名发送Post
- Android中发送网络请求时参数为中文,导致请求的结果不正确的解决方法
- Android网络框架-Volley(四) 使用get和post方法发送json请求
- Android客户端发送Post/Get请求到Asp.Net服务端一般处理程序Asp.Net进行参数解析
- Android中的http带参数网络请求,GET和POST
- Android Volley网络请求框架 实现post方法并带Map参数上传
- 【Android网络请求】如何使用Volley发送POST请求
- Android用httpURLConnection发送post网络请求并拿到数据
- Android Volley网络请求框架 实现post方法并带Map参数上传
- Android post请求网络数据(json作为参数)
- Android核心基础-7.Android 网络通信-5.发送GET、POST请求
- Android中利用HttpURLConnection发送Post请求并添加参数的写法
- Android Retrofit2 Post请求添加Json类型参数笔记
- Android get/post方式请求网络设置超时
- android发送http post请求函数带参数带返回数据 静态类 源码
- Android网络框架-Volley(四) 使用get和post方法发送json请求