美文网首页
POST提交方式

POST提交方式

作者: Raalstalblack | 来源:发表于2017-02-13 23:29 被阅读0次

客户端

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();
    
}

相关文章

网友评论

      本文标题:POST提交方式

      本文链接:https://www.haomeiwen.com/subject/ezotwttx.html