服务器端代码
[java]
package org.xiazdong.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/PrintServlet")
public class PrintServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
String age = new String(request.getParameter("age").getBytes("ISO-8859-1"),"UTF-8");
System.out.println("姓名:"+name+"\n年龄:"+age);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
System.out.println("姓名:"+request.getParameter("name")+"\n年龄:"+request.getParameter("age"));
}
}
Android端代码
[java]
package org.xiazdong.network.submit;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText name, age;
private Button getbutton, postbutton;
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
try{
if (getbutton == v) {
/*
* 因为是GET请求,所以需要将请求参数添加到URL后,并且还需要进行URL编码
* URL = http://192.168.0.103:8080/Server/PrintServlet?name=%E6%88%91&age=20
* 此处需要进行URL编码因为浏览器提交时自动进行URL编码
* */
StringBuilder buf = new StringBuilder("http://192.168.0.103:8080/Server/PrintServlet");
buf.append("?");
buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");
buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));
URL url = new URL(buf.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
Toast.makeText(MainActivity.this, "GET提交成功", Toast.LENGTH_SHORT).show();
}
else Toast.makeText(MainActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();
}
if (postbutton == v) {
/*
* 如果是POST请求,则请求参数放在请求体中,
* name=%E6%88%91&age=12
*
* */
StringBuilder buf = new StringBuilder();
buf.append("name="+URLEncoder.encode(name.getText().toString(),"UTF-8")+"&");
buf.append("age="+URLEncoder.encode(age.getText().toString(),"UTF-8"));
byte[]data = buf.toString().getBytes("UTF-8");
URL url = new URL("http://192.168.0.103:8080/Server/PrintServlet");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true); //如果要输出,则必须加上此句
OutputStream out = conn.getOutputStream();
out.write(data);
if(conn.getResponseCode()==200){
Toast.makeText(MainActivity.this, "GET提交成功", Toast.LENGTH_SHORT).show();
}
else Toast.makeText(MainActivity.this, "GET提交失败", Toast.LENGTH_SHORT).show();
}
}
catch(Exception e){
} www.2cto.com
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) this.findViewById(R.id.name);
age = (EditText) this.findViewById(R.id.age);
getbutton = (Button) this.findViewById(R.id.getbutton);
postbutton = (Button) this.findViewById(R.id.postbutton);
getbutton.setOnClickListener(listener);
postbutton.setOnClickListener(listener);
}
}
相关推荐
httpclient的用法,发送get请求和post请求,设置header
android发送get,post请求工具类
Android中Https请求get和post 博客地址:http://blog.csdn.net/lsf1025995457/article/details/51794377
超文本传输协议(HTTP)的...两种 HTTP 请求方法:GET 和 POST 在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST。 GET - 从指定的资源请求数据。 POST - 向指定的资源提交要被处理的数据
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
get方式请求头和请求体 2、请求参数可见性 index.php?content=这是get方式里面的一个字段的值 GET和POST使用区别 在POST请求中,查询字符串是在 POST 请求的 HTTP 消息主体中发送的 POST方式请求头和请求体 2、请求...
http协议是互联网上应用最为广泛的一种网络协议,他在接口中扮演着重要的角色,Post/Get请求,想必大家都有所耳闻,我们一起利用Boost::Asio库来实现Post/Get请求的发送。 VS2013 文章地址:...
本代码是httpclient发送get请求和post请求demo,具体描述参考博客 HttpClient发送get请求和post请求
使用java发送get和post请求实践
httpclient实现的工具类含有以下功能: 1、后台模拟发送GET请求 2、后台模拟发送POST请求 3、解决了中文乱码
GET 和 POST 是我们最常用的两种请求方式,今天讲一讲如何在 golang 服务中,正确接收这两种请求的参数信息。 处理GET请求 1.1 接收GET请求 //接收GET请求 func Get(writer http.ResponseWriter , request *...
C# http Get/POST请求封装类
HttpClient(用CloseableHttpClient发送get/post请求)
用HTML页面模拟测试get/post请求,psot请求参数的格式为json串
VC++ HTTP Get Post请求,自己编译就知道了,跟给力。。
利用Boost::Asio库来实现Post/Get请求的发送。可以快速开发c++的http请求,访问以http为协议的服务器示例代码。
封装了https发送Get和Post请求工的具类。直接看Demo例子。样例实现参考 HTTP服务POST方式代码
Get是最常用的方法,通常用于请求服务器发送某个资源,而且应该是安全的和幂等的。 (1). 所谓安全是指该操作用于获取信息而非修改信息。换句话说,GET 请求一般不应产生副作用。就是说,它仅仅是获取资源信息,就像...
C++实现HTTP的GET,POST请求及HTTPS的请求
50行代码http发送Post/Get请求,直接在linux gcc编译即可使用,或在win下任意c/c++编译器都可