HttpGet和HttpPost

 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 OnClickListener
26 {
27
28     @Override
29     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         try
36         {
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<NameValuePair> params = new ArrayList<NameValuePair>();
56                     params.add(new BasicNameValuePair("bookname", etBookName
57                             .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(httpResponse
64                                 .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     @Override
78     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 }
时间: 2024-10-06 13:49:46

HttpGet和HttpPost的相关文章

java最简单的方式实现httpget和httppost请求

java实现httpget和httppost请求的方式多种多样,个人总结了一种最简单的方式,仅仅需几行代码,就能够完美的实现. 此处须要用到两个jar包,httpclient-4.3.1.jar.httpcore-4.3.jar,各位能够到网上自己下载,或者到我的附件里下载,以下先贴上httpget请求的代码: String url = ""; HttpGet request = new HttpGet(url); try { HttpResponse response = HttpC

转-浅谈HTTP-GET 、 HTTP-POST 和SOAP

原文链接:浅谈HTTP-GET . HTTP-POST 和SOAP 1.HTTP-GET 和 HTTP-POST HTTP-GET和HTTP-POST是标准协议,他们使用HTTP(超文本传输协议)谓词(谓词是指条件表达式的求值返回真或假的过程.)对参数金星编码并将参数作为名称/值对传递,还使用关联的请求语义.每个协议都包含一系列HTTP请求标头,HTTP请求标头及其他一些信息定义客户端向服务器请求哪些内容,哪个服务器用一系列HTTP响应标头和所请求的数据进行响应. HTTP-GET 使用 MIM

Android中使用HttpGet和HttpPost访问HTTP资源

需求:用户登录(name:用户名,pwd:密码) (一)HttpGet :doGet()方法//doGet():将参数的键值对附加在url后面来传递 [java] view plaincopy public String getResultForHttpGet(String name,String pwd) throws ClientProtocolException, IOException{ //服务器  :服务器项目  :servlet名称 String path="http://192.

HTTPClient模块的HttpGet和HttpPost

无论是使用HttpGet,还是使用HttpPost,都必须通过如下3步来访问HTTP资源. 1.创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象. 2.使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST请求,并返回HttpResponse对象. 3.通过HttpResponse接口的getEntity方法返回响应信息,并进行相应的处理. 如果使用HttpPost方法提交HTTP POS

.net在Controller里的方法添加[HttpGet]和[HttpPost]

前端用post过来,Controller就要用[HttpPost],前端用get,Controller就要用[HttpGet],或者不管前端用什么,Controller都不加这些. 前端用post $.ajax({ url: "/CommRecord/LockSendESMS", type: "post", data: { id: data.ID, isLock: isLock }, success: function (data) { } }); Controll

Android:HttpGet与HttpPost

AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET"/> activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&

HttpGet和HttpPost请求

internal static string HttpPost(string Url, string postDataStr) { string retString = string.Empty; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = "application/x-www-form

linux下HttpGet、HttpPost的C++实现

#include "HttpRequest.h" int main() { HttpRequest* Http = new HttpRequest; char* str = (char*)malloc(BUFSIZE); memset(str, 0, BUFSIZE); if(Http->HttpGet("http://www.baidu.com", str)) { printf("%s\n", str); } else { printf(

HttpGet和HttpPost处理重定向的区别

get方法默认会处理302的重定向,response获取到的页面其实是重定向以后的页面,通过response.getStatusLine(),取到的值是200. 通过设置可以用post方法去请求或者把get自动处理重定向禁掉. 要禁用get方法自动处理重定向,需要设一下参数,如: Java代码   HttpClient httpclient = new DefaultHttpClient(); HttpParams params = httpclient.getParams(); params.