客户端
Post提交方式:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.0.2.2:8080/StudentAppService/RegisterServlet");
ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("name","liujie"));
post.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));
HttpResponse reponse = client.execute(post);
if(reponse.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
byte[] b = new byte[1024];
int length = -1;
String s = "";
while((length=is.read(b))!=-1){
s += new String(b,0,length);
}
}
服务器端:
服务器端接收客户端的数据,并且向客户端发送数据
request代表客户端给服务器发送的数据
reponse代表服务器给客户端发送数据
public void doPost(HttpServletRequest request,HttpServletResponse response){
String name = request.getParameter("name");
PrintWriter pw = response.getWriter();
pw.println("successfull");
pw.flush();
pw.close();
}
网友评论