package com.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import net.sf.json.JSONObject;
public class Login {
private static final String userName=FileUtils.readPropertiesFile("zhUserName", Constant.FILE_NAME, true);
private static final String password=FileUtils.readPropertiesFile("zhPwd", Constant.FILE_NAME, true);
private static final String url=FileUtils.readPropertiesFile("testUrl", Constant.FILE_NAME, true);
private static final Logger log = Logger.getLogger(Login.class);
public String getToken(){
CloseableHttpClient httpClient=HttpClients.createDefault();
HttpPost httpPost=new HttpPost(url+"/open_api/auth/partner_token/");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
JSONObject jsonObject=new JSONObject();
jsonObject.put("identity", userName);
jsonObject.put("password", password);
StringEntity stringEntity=new StringEntity(jsonObject.toString(), "utf-8");
httpPost.setEntity(stringEntity);
CloseableHttpResponse response=null;
String jsonString="";
String result="";
try {
response=httpClient.execute(httpPost);
StatusLine statusLine=response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
log.info("return data: " + jsonString);
} else {
log.error("error message:" + EntityUtils.toString(response.getEntity()));
}
} catch (IOException e) {
e.printStackTrace();
log.error("happan an error when POST data: " + e.getMessage());
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(jsonObject!=null){
JSONObject json=JSONObject.fromObject(jsonString);
result=json.get("token").toString();
//修改config中得token
InputStream inputStream=null;
OutputStream outputStream=null;
try {
inputStream=new FileInputStream(new File(Thread.currentThread().getContextClassLoader()
.getResource("config.properties").getFile()));
Properties properties=new Properties();
properties.load(inputStream);
properties.setProperty("token", result);
outputStream=new FileOutputStream(new File(Thread.currentThread().getContextClassLoader()
.getResource("config.properties").getFile()));
properties.store(outputStream, "");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return result;
}
public static void main(String[] args) {
Login login=new Login();
System.out.println(login.getToken());
String token=FileUtils.readPropertiesFile("token", Constant.FILE_NAME, true);
System.out.println(token);
}
}
如何将服务器返回的token信息保存到config下?
最新推荐文章于 2023-05-06 11:16:23 发布