通过HttpURLConnection回调三方链接验证登录

package com.lx.httpurlutils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("/a")
public class Login {
    @Autowired
    private HttpUtil httpUtil;
    @RequestMapping("/a")
    public void login(String userId , String userName){
        String parmString = "userId="+userId+"&userName="+userName;
        String contentType = "application/json";//可根据实际情况设置发送报文格式
        String url = "xxxxxxxxxxxx";
        try {
            String s = HttpUtil.doPost(url, parmString, contentType);
            //这里根据回调回来的字符串s与正确的字符串进行比对,确认是否可以登录
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package com.lx.httpurlutils;

import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

@Component
public class HttpUtil {
    //POST请求
    public static String doPost(String url , String parmString , String contentType) throws Exception {
        //获取连接
        URL _url = new URL(url);
        URLConnection urlConnection = _url.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;
        //设置请求头
        httpURLConnection.setDoOutput(true);// 使用 URL 连接进行输出,默认为“false”
        httpURLConnection.setDoInput(true);// 使用 URL 连接进行输入,默认为“true”
        httpURLConnection.setUseCaches(false);// 忽略缓存
        httpURLConnection.setRequestMethod("POST");// 设置URL请求方法,默认为“GET”
        httpURLConnection.setRequestProperty("Accept-Charset","utf-8");
        httpURLConnection.setRequestProperty("Content-Type",contentType);//设置的文本类型,此字段必须和和服务器端处理请求流的编码一致,否则无法解析

        OutputStream outputStream = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String template = null;
        try{
            //将参数存储在流中中,最后和前面设置的“请求头”组装成request对象,进行访问
            outputStream = httpURLConnection.getOutputStream();
            outputStream.write(parmString.getBytes("utf-8"));
            outputStream.flush();
            //获取响应信息
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode >= 300){
                throw new Exception("请求失败"+httpURLConnection.getResponseMessage());
            }
            //获取响应内容
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            reader = new BufferedReader(inputStreamReader);
            while ((template = reader.readLine()) != null){
                resultBuffer.append(template);
            }
        }finally {
            if (outputStream != null){
                outputStream.close();
            }
            if (inputStream != null){
                inputStream.close();
            }
            if (inputStreamReader != null){
                inputStreamReader.close();
            }
            if (reader != null){
                reader.close();
            }
        }

        return resultBuffer.toString();
    }
}

注:

1. 该案例回调方式为POST,请求参数需要以上述方式放到输出流中进行访问

2. 将回调回来的内容进行比对,若一致,则登录成功

3. 报文格式Content-Type,此字段必须和服务器端处理请求流的编码一致,否则无法解析

4. 若回调方式改为GET。则传参方式不需要使用流来传输,只需以GET请求方式拼接到url后

5. 回调返回的内容根据实际情况接受,该案例返回为字符串

String url = "https://www.baidu.com?userId="+userId+"&userName="+userName+"";


版权声明:本文为weixin_60589038原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。