get访问--url

本文提供了一个使用Java发送GET请求的示例,并展示了如何在服务器端解析这些请求参数。客户端部分通过构造特定URL来发送GET请求,而服务器端则通过读取请求参数并打印出来进行响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

get 请求 :

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by admin on 2018/7/18.
 */
public class test01 {


    public static void main(String[] args) {


        String urlMessage = "http://localhost:8086/LoveCartoon_ZC/testNotify?name=张三&sex=男&age=20";
        try {
            String result = doGet(urlMessage);
            System.out.println("result=" + result);
        }catch (Exception e){

        }


    }

    public static String doGet(String urlMessage) throws Exception {

        URL url = new URL(urlMessage.toString());
        System.out.println("url=============" + urlMessage);
        HttpURLConnection urlConnection = null;
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("Accept", "application/json,application/xml,text/html");
        urlConnection.setRequestProperty("Connection", "Keep-Alive");
        urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
        urlConnection.setRequestProperty("Content-type", "text/html;charset=UTF-8");
        urlConnection.setReadTimeout(30000);
        urlConnection.setConnectTimeout(20000);
        urlConnection.connect();
        String result = "";
        BufferedReader in = null;
        in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
        String line = null;
        while ((line = in.readLine()) != null) {
            result += line;
        }

        return result;
    }


}

运行结果:

 

接收参数:

import com.alibaba.fastjson.JSON;
import com.xtone.lovecartoon.csmtp.servlet.DaoServlet;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class testNotify extends DaoServlet {
    public static Logger logger = Logger.getLogger("testNotify");


    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws IOException, ServletException {
        doGet(request, response);

    }


    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws IOException, ServletException {


        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
/


        try {
            out.print("OK");
            out.flush();
            out.close();


            System.out.println("请求参数: " + JSON.toJSONString(request.getParameterMap()));
            String name = getParam("name", request);
            String age = getParam("age", request);
            String sex = getParam("sex", request);
            System.out.println("name=" + name);
            System.out.println("age=" + age);
            System.out.println("sex=" + sex);

        } catch (Exception e) {
            logger.info("Error:" + e.getMessage());
            e.printStackTrace();
        }


    }



    private String getParam(String key, HttpServletRequest request) {
        try {
            return request.getParameter(key).toString();
        } catch (Exception ex) {
            return "";
        }
    }

运行结果:

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值