请求不同端口的接口

请求端代码:

  1、jsp页面代码

<button id="btn" onclick="test()">端口测试</button>

<script type="text/javascript">
    function test(){
        alert(1212)
        var url="/checkCert";
        //var args={""};
        $.post(url, function(data) {
            
        }, "json");
    
    }
</script >

  2、后端代码

package com.xjgzinfo.casso.servlet.checkcertservlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

public class CheckCertServlet  extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		System.out.println("1212121");
		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
				try {
					this.httpPost(null);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
		}
		
		
	
	
	private String httpPost(JSONObject jsonParam) throws Exception {
		String URL = "http://192.168.117.140:8083/getAllTbUserCert";
		String jsonString = "";
		
		// 创建连接
		URL url = new URL(URL);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setRequestMethod("POST"); // 设置请求方法
		connection.setRequestProperty("Charsert", "UTF-8"); // 设置请求编码
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
		connection.setRequestProperty("Content-Type", "application/json");

		connection.connect();

		/*PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "utf-8"));
		out.println(jsonParam.toString());*/

		/*out.flush();
		out.close();*/

		// 读取响应 返回值为200输出的响应信息
		if (connection.getResponseCode() == 200) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				sb.append(lines);
			}
			
			jsonString = sb.toString().replaceAll(" ", "");
			reader.close();
		}

		// 读取响应 返回值为400输出的响应信息
		if (connection.getResponseCode() == 400) {
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(connection.getErrorStream()));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				sb.append(lines);
			}
			jsonString = sb.toString();
			reader.close();
		}
		
		// 断开连接
		connection.disconnect();
			
		return jsonString;
		
	}
}

  

请求端代码:被请求端代码

  @RequestMapping("/getAllTbUserCert")
    @ResponseBody
    public List<TbUserCert> getAllTbUserCert(){
        List<TbUserCert> tbUserCerts = tbUserCertService.getTbUserCert();
        System.out.println("----------------------------------------------");
        return tbUserCerts;
    }

注意:端口设置为8083

 

posted @ 2021-08-08 00:56  极地阳光-ing  阅读(184)  评论(0编辑  收藏  举报