博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpGet和HttpPost
阅读量:5323 次
发布时间:2019-06-14

本文共 5129 字,大约阅读时间需要 17 分钟。

1 package net.blogjava.mobile; 2  3 import java.net.HttpURLConnection; 4 import java.util.ArrayList; 5 import java.util.List; 6  7 import org.apache.http.HttpResponse; 8 import org.apache.http.NameValuePair; 9 import org.apache.http.client.entity.UrlEncodedFormEntity;10 import org.apache.http.client.methods.HttpGet;11 import org.apache.http.client.methods.HttpPost;12 import org.apache.http.impl.client.DefaultHttpClient;13 import org.apache.http.message.BasicNameValuePair;14 import org.apache.http.protocol.HTTP;15 import org.apache.http.util.EntityUtils;16 17 import android.app.Activity;18 import android.os.Bundle;19 import android.view.View;20 import android.view.View.OnClickListener;21 import android.widget.Button;22 import android.widget.EditText;23 import android.widget.TextView;24 25 public class Main extends Activity implements OnClickListener26 {27 28     @Override29     public void onClick(View view)30     {31         String url = "http://169.254.103.188/querybooks/QueryServlet";32         TextView tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);33         EditText etBookName = (EditText) findViewById(R.id.etBookName);34         HttpResponse httpResponse = null;35         try36         {37             38             switch (view.getId())39             {40                 case R.id.btnGetQuery:41                     url += "?bookname=" + etBookName.getText().toString();42                     HttpGet httpGet = new HttpGet(url);43                     httpResponse = new DefaultHttpClient().execute(httpGet);44                     if (httpResponse.getStatusLine().getStatusCode() == 200)45                     {46                         47                         String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);48                         tvQueryResult.setText(result);49                         System.out.println(result);50                     }51                     break;52 53                 case R.id.btnPostQuery:54                     HttpPost httpPost = new HttpPost(url);55                     List
params = new ArrayList
();56 params.add(new BasicNameValuePair("bookname", etBookName57 .getText().toString()));58 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));59 60 httpResponse = new DefaultHttpClient().execute(httpPost);61 if (httpResponse.getStatusLine().getStatusCode() == 200)62 {63 String result = EntityUtils.toString(httpResponse64 .getEntity());65 tvQueryResult.setText(result.replaceAll("\r", ""));66 }67 break;68 }69 }70 catch (Exception e)71 {72 tvQueryResult.setText(e.getMessage());73 }74 75 }76 77 @Override78 public void onCreate(Bundle savedInstanceState)79 {80 super.onCreate(savedInstanceState);81 setContentView(R.layout.main);82 Button btnGetQuery = (Button) findViewById(R.id.btnGetQuery);83 Button btnPostQuery = (Button) findViewById(R.id.btnPostQuery);84 btnGetQuery.setOnClickListener(this);85 btnPostQuery.setOnClickListener(this);86 87 }88 }

 上传文件时,可以使用HttpAnalyzer来捕获HTTP请求信息:上传文档新建文本文档 (3).txt过程:

Request Headers    Value(Request-Line)    POST /upload/UploadServlet HTTP/1.1Accept    text/html, application/xhtml+xml, */*Referer    http://localhost/upload/upload.jspAccept-Language    zh-CNUser-Agent    Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)Content-Type    multipart/form-data; boundary=---------------------------7dea05100618Accept-Encoding    gzip, deflateHost    localhostContent-Length    239DNT    1Connection    Keep-AliveCache-Control    no-cacheCookie    JSESSIONID=C55149A1C7C5EEFE0A67D4DA2183E8BF
Response Headers    Value(Status-Line)    HTTP/1.1 200 OKServer    Apache-Coyote/1.1Content-Type    text/html;charset=UTF-8Content-Length    21Date    Sun, 14 Dec 2014 07:00:05 GMT

stream标签页信息:

744 bytes sent to :0

POST /upload/UploadServlet HTTP/1.1Accept: text/html, application/xhtml+xml, */*Referer: http://localhost/upload/upload.jspAccept-Language: zh-CNUser-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)Content-Type: multipart/form-data; boundary=---------------------------7dea05100618Accept-Encoding: gzip, deflateHost: localhostContent-Length: 239DNT: 1Connection: Keep-AliveCache-Control: no-cacheCookie: JSESSIONID=C55149A1C7C5EEFE0A67D4DA2183E8BF-----------------------------7dea05100618Content-Disposition: form-data; name="file"; filename="C:\Users\Administrator\Desktop\鏂板缓鏂囨湰鏂囨。 (3).txt"Content-Type: text/plain123abc-----------------------------7dea05100618--

 

163 bytes received by :0

HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Type: text/html;charset=UTF-8Content-Length: 21Date: Sun, 14 Dec 2014 07:00:05 GMT鏂囦欢涓婁紶鎴愬姛!

 

转载于:https://www.cnblogs.com/hixin/p/4162490.html

你可能感兴趣的文章
浅谈性能测试
查看>>
Winform 菜单和工具栏控件
查看>>
jequery动态创建form
查看>>
CDH版本大数据集群下搭建的Hue详细启动步骤(图文详解)
查看>>
巧用Win+R
查看>>
浅析原生js模仿addclass和removeclass
查看>>
Python中的greenlet包实现并发编程的入门教程
查看>>
java中遍历属性字段及值(常见方法)
查看>>
深入理解jQuery框架-框架结构
查看>>
YUI3自动加载树实现
查看>>
python知识思维导图
查看>>
当心JavaScript奇葩的逗号表达式
查看>>
App Store最新审核指南(2015年3月更新版)
查看>>
织梦MIP文章内容页图片适配百度MIP规范
查看>>
点击复制插件clipboard.js
查看>>
[Kali_BT]通过低版本SerialPort蓝牙渗透功能手机
查看>>
C语言学习总结(三) 复杂类型
查看>>
HNOI2018
查看>>
【理财】关于理财的网站
查看>>
Ubunt中文乱码
查看>>