zoukankan      html  css  js  c++  java
  • 模拟手机客户端与Web应用的交互[URL和HttpURLConnection的使用]

    URL和HttpURLConnection的使用

      java.lang.Object

        |--java.net.URL

      java.lang.Object

        |--java.net.URLConnection

          |--java.net.HttpURLConnection

      1、模拟手机端

     1 import java.io.OutputStream;
     2 import java.io.PrintWriter;
     3 import java.net.HttpURLConnection;
     4 import java.net.URL;
     5 import java.net.URLEncoder;
     6 
     7 //模拟手机客户端
     8 public class Phone {
     9     public static void main(String[] args) throws Exception {
    10         //建立与服务端的连接
    11         URL url = new URL("http://localhost:8080/day26/Server");
    12         //取得与服务端的连接
    13         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    14         conn.setDoOutput(true);//防止报Exception in thread "main" java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
    15         //向服务端发送请求
    16         OutputStream os = conn.getOutputStream();
    17         PrintWriter pw = new PrintWriter(os);
    18         String username = "杰克";
    19         username = URLEncoder.encode(username,"UTF-8");
    20         pw.write("username="+username+"&password=12346");
    21         pw.flush();
    22         pw.close();
    23         //通知服务端客户端已发送请求完毕
    24         int code = conn.getResponseCode();
    25         System.out.println("code=" + code);
    26         
    27         /*接收服务端的输出
    28         InputStream is = conn.getInputStream();
    29         byte[] buf = new byte[1024];
    30         int len = 0;
    31         while((len=is.read(buf))>0){
    32             System.out.println(new String(buf,0,len));
    33         }
    34         //关闭
    35         is.close();
    36         */
    37     }
    38 }

       2、服务端

     1 import java.io.IOException;
     2 import java.util.Date;
     3 
     4 import javax.servlet.ServletException;
     5 import javax.servlet.http.HttpServlet;
     6 import javax.servlet.http.HttpServletRequest;
     7 import javax.servlet.http.HttpServletResponse;
     8 
     9 public class Server extends HttpServlet {
    10     public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    11         //输出到客户端
    12         response.setContentType("text/html;charset=GBK");
    13         response.getWriter().write("当前时间为[GET]:" + new Date().toLocaleString());
    14     }
    15     public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    16         //接收客户端的请求
    17         request.setCharacterEncoding("UTF-8");
    18         String username = request.getParameter("username");
    19         String password = request.getParameter("password");
    20         System.out.println("username[POST]="+username);
    21         System.out.println("password[POST]="+password);
    22     }
    23 }

     总结:

    1)服务端[GET]->手机--GET方式

    (1)手机先发送请求,再接收服务端的响应
    (2)在服务端写到GET方式中

      2)手机->服务端[POST]--POST方式
        (1)在服务端写到POST方式中

      3)对于中文方式

    (1)通过URLEncoder进行URL编码,
      String username = "杰克";
      username = URLEncoder.encode(username,"UTF-8");
    (2)在服务端进行编码设置:
      request.setCharacterEncoding("UTF-8");
    (3)必须确保URL编码和解析一致

  • 相关阅读:
    ubuntu安装
    k8s 安装
    Blazor 路由
    ISO 8601
    Centos 8使用devstack快速安装openstack最新版
    使用devstack 一键安装 openstack详细过程和遇到的坑
    蓝瑟66000公里保养
    释放rsyslog占用的Linux内存
    Netty4.1 Http开发入门(一)服务端
    网络传输中的帧和payload
  • 原文地址:https://www.cnblogs.com/hacket/p/3052834.html
Copyright © 2011-2022 走看看