zoukankan      html  css  js  c++  java
  • 【转】 Pro Android学习笔记(六八):HTTP服务(2):HTTP POST

    目录(?)[-]

    1. 找一个测试网站
    2. HTTP POST小例子

    上次学习了HTTP GET请求,这次学习一下HTTP POST。

    找一个测试网站

    小例子好写,但要找个测试网站就有些麻烦,一下子无从入手,都考虑是否下个Apache,自己弄一个。最后找了个论文查询网站,结果form内容很长,而且很多(少的话就直接用GET)。于是下了个WireShare进行抓包,如下,直接从HTTP的请求消息中看要传递哪些参数。(PS:参数有点多,总比弄个web server要强点)

    HTTP POST小例子

    HTTP POST和HTTP GET的效力很似,不同的是client的execute()参数是HttpPost实例,而非HttpGet实例。具体如下:

    BufferedReader in = null; 
    try{ 
        //【Step 1】创建一个HttpClient的对象(或使用已有的) 
        HttpClient client = new DefaultHttpClient(); 
        //【Step 2】实例化一个HTTP GET或者HTTP POST,本例是HTTP POST 
        HttpPost request = new HttpPost("http://epub.cnki.net/kns/brief/default_result.aspx"); 
         
        //【Step 3】设置HTTP参数,本例根据抓包的内容填写,这是体力活,在完整HTTP服务的笔记后,会提供小例子下载。对于HTTP Post,需要传递键值对信息,从上面的转包可以看到,这部分不是作为request URI,而是作为HTML Form URL Encoded,为此我们需要用户元素为NameValuePair格式的list来存储这些信息,并封装在UrlEncodedFormEntiry对象中。通过setEntity()加入到请求对象中。
        List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair(
    "txt_1_sel","TI$%=|"));
        postParameters.add(new BasicNameValuePair("txt_1_value1","Android"));  
        … … 
        postParameters.add(new BasicNameValuePair("ua","1.11"));      
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);
     
        
        //【Step 4】通过HttpClient来执行HTTP call(发出HTTP请求)
        HttpResponse response =client.execute(request); 
        
        //【Step 5】处理HTTP响应,本例将整个响应的内容(HTTP 200消息的body)都在String中。 
        in = new BufferedReader( 
                new InputStreamReader( 
                        response.getEntity().getContent())); 
        
        StringBuffer buff = new StringBuffer(""); 
        String line = ""; 
        String NL = System.getProperty("line.separator"); 
        while((line = in.readLine())!= null){ 
            buff.append(line + NL); 
        } 
        showInfo(buff.toString());     
    }catch(Exception e){ 
        e.printStackTrace(); 
        showInfo(e.toString()); 
    }finally{ 
        if(in != null){ 
            try{ 
                showInfo("== process in.colse() =="); 
                in.close(); 
            }catch(Exception e){ 
                e.printStackTrace(); 
                showInfo(e.toString()); 
            } 
        }    
    }

    本博文涉及的例子代码,可以在Pro Android学习:Http service小例子中下载。

    相关链接: 我的Android开发相关文章

  • 相关阅读:
    IE9不能在线打开InfoPath表单的解决办法
    在Word中使用Quick Parts功能
    .NET WinForm程序在Windows7下实现玻璃效果和任务栏进度条效果
    如何对已经发布过的InfoPath模板进行修改
    .NET WinForm中给DataGridView自定义ToolTip并设置ToolTip的样式
    有关SharePoint Client Object应用的笔记
    解决VS 2010中编译程序时弹出"Type universe cannot resolve assembly"的错误
    SharePoint客户端对象模型"(400) Bad Request"错误
    C# Method Attribute and Reflection
    .NET WinForm下一个支持更新ProgressBar进度的DataGridView导出数据到Excel的类
  • 原文地址:https://www.cnblogs.com/blongfree/p/5048042.html
Copyright © 2011-2022 走看看