• java后端发送请求


    package com.ty.mapapisystem.util;

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    import com.ty.mapapisystem.controller.LoginController;

    import net.sf.json.JSONObject;

    public class DynamicReadConfig {
    /**
    * 获取配置文件的属性
    * @param configPath 配置文件路径
    * @param propertyName 要获取该属性的值
    * @return 读取的属性值
    * @throws Exception
    * @throws ConfigurationException 异常向上抛出,有调用该方法的用户进行处理
    */
    public static Log log=LogFactory.getLog(DynamicReadConfig.class);
    public static String getConfigProperty(String configPath,String propertyName) {
    InputStream in = null;
    try {
    in = new FileInputStream(configPath);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    Properties prop = new Properties();
    try {
    prop.load(in);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    String s = prop.getProperty(propertyName);
    return s;
    }

    public static String startPost(String url,Map<String,Object> map){
    StringBuffer sb = new StringBuffer();
    for(Map.Entry<String , Object> data:map.entrySet()){
    sb.append(data.getKey()+"="+data.getValue()+"&");
    }
    String rtn = sb.toString().substring(0, sb.toString().length()-1);
    return sendPost(url,rtn);
    }
    public static String sendPost(String url, String param) {

    PrintWriter out = null;

    BufferedReader in = null;

    String result = "";

    try {

    URL realUrl = new URL(url);

    // 打开和URL之间的连接

    URLConnection conn = realUrl.openConnection();

    // 设置通用的请求属性

    conn.setRequestProperty("accept", "*/*");

    conn.setRequestProperty("connection", "Keep-Alive");

    conn.setRequestProperty("user-agent",

    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

    // 发送POST请求必须设置如下两行

    conn.setDoOutput(true);

    conn.setDoInput(true);

    // 获取URLConnection对象对应的输出流

    out = new PrintWriter(conn.getOutputStream());

    // 发送请求参数

    out.print(param);

    // flush输出流的缓冲

    out.flush();

    // 定义BufferedReader输入流来读取URL的响应

    in = new BufferedReader(

    new InputStreamReader(conn.getInputStream(),"utf-8"));

    String line;

    while ((line = in.readLine()) != null) {

    result += line;

    }

    } catch (Exception e) {

    log.error("发送 POST 请求出现异常!",e);

    e.printStackTrace();

    }

    //使用finally块来关闭输出流、输入流

    finally{

    try{

    if(out!=null){

    out.close();

    }

    if(in!=null){

    in.close();

    }

    }

    catch(Exception ex){

    ex.printStackTrace();

    }

    }

    return result;

    }


    public static String oauth(String account,String passWord) throws Exception {
    String url = "";
    String id = "";
    String secret = "";
    String type = "";
    String scope = "";
    String configPro =DynamicReadConfig.class.getResource("/").getPath() + "/oauthConfig.properties";//"E:\MapAPISystem(1)\MapAPISystem\src\oauthConfig.properties";
    try {
    url = DynamicReadConfig.getConfigProperty(configPro, "login.url");
    id = DynamicReadConfig.getConfigProperty(configPro, "login.id");
    secret = DynamicReadConfig.getConfigProperty(configPro, "login.secret");
    type = DynamicReadConfig.getConfigProperty(configPro, "login.type");
    scope = DynamicReadConfig.getConfigProperty(configPro, "login.scope");
    } catch (Exception e) {
    e.printStackTrace();
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(id.split(";")[0], id.split(";")[1]);
    map.put(secret.split(";")[0], secret.split(";")[1]);
    map.put(type.split(";")[0], type.split(";")[1]);
    map.put("username", account);
    map.put("password", passWord);
    map.put(scope.split(";")[0], scope.split(";")[1]);
    String str = DynamicReadConfig.startPost(url, map);
    if(str == ""){
    return "";
    }
    String ToKen = "";
    JSONObject json = JSONObject.fromObject(str);

    ToKen=json.getString("access_token");//String.valueOf(map1.get("access_token"));
    // }
    return ToKen;
    }

    //
    // public static void main(String[] args) throws Exception {
    // String str = DynamicReadConfig.oauth("gh", "123");
    // System.out.println(str);
    // }
    }

  • 相关阅读:
    R语言:文本(字符串)处理与正则表达式
    RStudio版本管理 整合Git
    GIT 图形化操作指南
    RStudio 断点调试 进入for循环语句调试
    安装python Matplotlib 库
    win7 32位安装 python 及Numpy、scipy、matplotlib函数包
    ubuntu下hive-0.8.1配置
    我对PageRank的理解及R语言实现
    R 数据类型
    Pig Latin JOIN (inner) 与JOIN (outer)的区别
  • 原文地址:https://www.cnblogs.com/qiyc/p/8425041.html
Copyright © 2020-2023  润新知