package BZB02;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.*;
import java.net.URL;
import java.net.URLConnection;
import java.sql.*;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.*;
import java.net.URL;
import java.net.URLConnection;
import java.sql.*;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import javax.naming.NamingException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONObject;
import org.dom4j.*;
import com.justep.baas.action.ActionContext;
import com.justep.baas.data.DataUtils;
import com.justep.baas.data.Row;
import com.justep.baas.data.Table;
import com.justep.baas.data.sql.SQLException;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.*;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import BZB03.GetDeptByPhone;
import com.justep.baas.data.DataUtils;
import com.justep.baas.data.Row;
import com.justep.baas.data.Table;
import com.justep.baas.data.sql.SQLException;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.*;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import BZB03.GetDeptByPhone;
public class Test {
// public static String syncPerson(){
public static JSONObject syncPerson(JSONObject params, ActionContext context)throws SQLException, NamingException, java.sql.SQLException {
System.out.println("1111111111111111111111111111111111111@@@@");
//发送 GET 请求
String s= Test.sendGet("https://qy.bangongyi.com/address/api/staff/list", "ticket=eb1b74051b7edaaf39d4396ec49d14a9");
System.out.println(s);
JSONObject doc = new JSONObject();//创建json对象
JSONObject jsonObject = JSONObject.fromObject(s);
//提取出error为 0
int errno = jsonObject.getInt("errno");
System.out.println("errno:" + errno);
//提取出status为 success
String errmsg = jsonObject.getString("errmsg");
System.out.println("errmsg:" + errmsg);
String data = jsonObject.getString("data");
JSONObject dataObject = JSONObject.fromObject(data);
System.out.println(dataObject.getString("count"));
//注意:results中的内容带有中括号[],所以要转化为JSONArray类型的对象
JSONArray result = dataObject.getJSONArray("staff_list");
for (int i = 0; i < result.size(); i++) {
//注意:index中的内容带有中括号[],所以要转化为JSONArray类型的对象
String staff_id = result.getJSONObject(i).getString("staff_id");
System.out.println("staff_id:" + staff_id);
String we_userid = result.getJSONObject(i).getString("we_userid");
System.out.println("we_userid:" + we_userid);
String we_avatar = result.getJSONObject(i).getString("we_avatar");
System.out.println("we_avatar:" + we_avatar);
String we_mobile = result.getJSONObject(i).getString("we_mobile");
System.out.println("we_mobile:" + we_mobile);
String we_name = result.getJSONObject(i).getString("we_name");
System.out.println("we_name:" + we_name);
String we_weixinid = result.getJSONObject(i).getString("we_weixinid");
System.out.println("we_weixinid:" + we_weixinid);
String we_gender = result.getJSONObject(i).getString("we_gender");
System.out.println("we_gender:" + we_gender);
String upStr = "update Sa_opperson s set s.staffID='"+staff_id+"' where s.sLoginName='"+we_mobile+"'";
//
Connection conn = context.getConnection("jeecg37bzb");//获取数据库连接
java.sql.Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
//
stmt.executeUpdate(upStr);
JSONObject dataObject = JSONObject.fromObject(data);
System.out.println(dataObject.getString("count"));
//注意:results中的内容带有中括号[],所以要转化为JSONArray类型的对象
JSONArray result = dataObject.getJSONArray("staff_list");
for (int i = 0; i < result.size(); i++) {
//注意:index中的内容带有中括号[],所以要转化为JSONArray类型的对象
String staff_id = result.getJSONObject(i).getString("staff_id");
System.out.println("staff_id:" + staff_id);
String we_userid = result.getJSONObject(i).getString("we_userid");
System.out.println("we_userid:" + we_userid);
String we_avatar = result.getJSONObject(i).getString("we_avatar");
System.out.println("we_avatar:" + we_avatar);
String we_mobile = result.getJSONObject(i).getString("we_mobile");
System.out.println("we_mobile:" + we_mobile);
String we_name = result.getJSONObject(i).getString("we_name");
System.out.println("we_name:" + we_name);
String we_weixinid = result.getJSONObject(i).getString("we_weixinid");
System.out.println("we_weixinid:" + we_weixinid);
String we_gender = result.getJSONObject(i).getString("we_gender");
System.out.println("we_gender:" + we_gender);
String upStr = "update Sa_opperson s set s.staffID='"+staff_id+"' where s.sLoginName='"+we_mobile+"'";
//
Connection conn = context.getConnection("jeecg37bzb");//获取数据库连接
java.sql.Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
//
stmt.executeUpdate(upStr);
// KSQL.executeUpdate(upStr, null, "/demo/test/data", null);
}
}
return doc;
}
}
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* Java将Unix时间戳转换成指定格式日期字符串
* @param timestampString 时间戳 如:"1473048265";
* @param formats 要格式化的格式 默认:"yyyy-MM-dd HH:mm:ss";
*
* @return 返回结果 如:"2016-09-05 16:06:42";
*/
public static String TimeStamp2Date(String timestampString) {
String formats = "yyyy-MM-dd HH:mm:ss";
Long timestamp = Long.parseLong(timestampString) * 1000;
String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
return date;
}
* Java将Unix时间戳转换成指定格式日期字符串
* @param timestampString 时间戳 如:"1473048265";
* @param formats 要格式化的格式 默认:"yyyy-MM-dd HH:mm:ss";
*
* @return 返回结果 如:"2016-09-05 16:06:42";
*/
public static String TimeStamp2Date(String timestampString) {
String formats = "yyyy-MM-dd HH:mm:ss";
Long timestamp = Long.parseLong(timestampString) * 1000;
String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
return date;
}
public static String getClockLog(JSONObject params, ActionContext context)throws SQLException, NamingException, java.sql.SQLException{
//同步数据
Connection conn = context.getConnection("jeecg37bzb");//获取数据库连接
java.sql.Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String strKsql = "select max(t.nextID) as nextID from T_sign t";
String simg1 = getimg();
System.out.println("输出的是"+strKsql);
// System.out.println("随机的数字是:"+simg1);
String nextID = "00000000";
Table table1 = DataUtils.queryData((java.sql.Connection) conn, strKsql, null, null, null, null);
Iterator<Row> iterator1 = table1.getRows().iterator();
while (iterator1.hasNext()) {
//遍历获取到的数据
Row info = iterator1.next();
nextID = info.getString("nextID");
System.out.println("nextID"+nextID);
}
//发送 GET 请求
String s= Test.sendGet("https://qy.bangongyi.com/attend/api/check/log", "ticket=eb1b74051b7edaaf39d4396ec49d14a9&start_time=2018-04-01&next_id="+nextID+"");//&next_id="+nextID+" &next_id="+nextID+"
System.out.println("发送的请求是"+s);
JSONObject jsonObject = JSONObject.fromObject(s);
//提取出error为 0
int errno = jsonObject.getInt("errno");
System.out.println("errno:" + errno);
//提取出next_id
int next_id = jsonObject.getInt("next_id");
System.out.println("next_id:" + next_id);
//提取出status为 success
String errmsg = jsonObject.getString("errmsg");
System.out.println("errmsg:" + errmsg);
//注意:results中的内容带有中括号[],所以要转化为JSONArray类型的对象
if(jsonObject.has("data")){
JSONArray result = jsonObject.getJSONArray("data");
System.out.println("能不能获取电话:" + result);
for (int i = 0; i < result.size(); i++) {
//注意:index中的内容带有中括号[],所以要转化为JSONArray类型的对象
String id = result.getJSONObject(i).getString("id");
System.out.println("id:" + id);
String time = result.getJSONObject(i).getString("time");
String date = TimeStamp2Date(time);
System.out.println("time:" + TimeStamp2Date(time));
String verify = result.getJSONObject(i).getString("verify");
System.out.println("verify:" + verify);
String staff_id = result.getJSONObject(i).getString("staff_id");
System.out.println("staff_id:" + staff_id);
String we_name = result.getJSONObject(i).getString("we_name");
System.out.println("we_name:" + we_name);
String we_userid = result.getJSONObject(i).getString("we_userid");
System.out.println("we_userid:" + we_userid);
String device_type = result.getJSONObject(i).getString("device_type");
System.out.println("device_type:" + device_type);
//fSignDate 2018-05-04 08:37:35
// 0123456789012345678
String signDate = date.substring(0, 10);
System.out.println(date.substring(0, 10)+" "+date.substring(11, 13));
String sweek = getWeekday(signDate);//星期
String simg = getimg();
String sdeoName = getDepetName();
System.out.println("随机的数字是:"+simg);
int hour = Integer.parseInt(date.substring(11, 13).replace("0", ""));
String upKsql = "";
boolean is = judgeSign(staff_id,signDate,context);
//获取人员岗位名称
com.alibaba.fastjson.JSONObject doc1 = new com.alibaba.fastjson.JSONObject();//创建json对象
if (staff_id != null && staff_id.equals("")) {
doc1 =BZB03.GetDeptByPhone.getDeptByPhone(staff_id, context);
}
String deptName = doc1.getString("deptName");//部门
String orgName = doc1.getString("orgName");//机构
String deptId = doc1.getString("deptId");//id
String orgId = doc1.getString("orgId");//id
System.out.println(is);
if(is){
if(hour < 12){
upKsql = "update T_sign t set t.start_sign_time='"+date+"' where t.staff_id='"+staff_id+"' and t.fSignDate='"+signDate+"'";
}else{
upKsql = "update T_sign t set t.end_sign_time='"+date+"' where t.staff_id='"+staff_id+"' and t.fSignDate='"+signDate+"'";
}
}else{
if(hour < 12){
upKsql = "insert into T_sign (fId,nextID,id,time,verify,staff_id,we_name,we_userid,device_type,start_sign_time,fSignDate,fWeek,fimg,deptName,orgName,deptId,orgId) "
+"values(uuid(),"+next_id+",'"+id+"','"+TimeStamp2Date(time)+"','"+verify+"','"+staff_id+"','"+we_name+"','"+we_userid+"','"+device_type+"','"+date+"','"+signDate+"','"+sweek+"','"+simg+"','"+sdeoName+"','"+orgName+"','"+deptId+"','"+orgId+"')";
}else{
upKsql = "insert into T_sign (fId,nextID,id,time,verify,staff_id,we_name,we_userid,device_type,end_sign_time,fSignDate,fWeek,fimg,deptName,orgName,deptId,orgId) "
+"values(uuid(),"+next_id+",'"+id+"','"+TimeStamp2Date(time)+"','"+verify+"','"+staff_id+"','"+we_name+"','"+we_userid+"','"+device_type+"','"+date+"','"+signDate+"','"+sweek+"','"+simg+"','"+sdeoName+"','"+orgName+"','"+deptId+"','"+orgId+"')";
}
}
System.out.println(upKsql);
//执行开始
stmt.executeUpdate(upKsql);
System.out.println("执行成功");
}
}
//
conn.close();
return s;
}
public static String getWeekday(String date){//必须yyyy-MM-dd
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdw = new SimpleDateFormat("E");
Date d = null;
try {
d = sd.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return sdw.format(d);
}
public static String getimg(){//生成随机数
String[] arr3=new String[]
{
"/x5/UI2/BZB01/project/project_clock_in/img/bk001.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk002.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk003.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk004.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk005.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk006.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk007.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk008.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk009.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk010.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk011.png"
};
Random ran2 = new Random();
int m = ran2.nextInt(10);
System.out.println("随机的数字是:"+m);
String c = arr3[m];
return c;
}
public static String getDepetName(){//生成随机数
String[] arr3=new String[]
{
"项目经理",
"项目副经理",
"项目总工",
"安全员",
"技术员",
"信息员",
"质量员",
"管理员",
};
Random ran2 = new Random();
int m = ran2.nextInt(7);
System.out.println("随机的数字是:"+m);
String c = arr3[m];
return c;
}
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdw = new SimpleDateFormat("E");
Date d = null;
try {
d = sd.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return sdw.format(d);
}
public static String getimg(){//生成随机数
String[] arr3=new String[]
{
"/x5/UI2/BZB01/project/project_clock_in/img/bk001.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk002.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk003.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk004.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk005.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk006.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk007.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk008.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk009.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk010.png",
"/x5/UI2/BZB01/project/project_clock_in/img/bk011.png"
};
Random ran2 = new Random();
int m = ran2.nextInt(10);
System.out.println("随机的数字是:"+m);
String c = arr3[m];
return c;
}
public static String getDepetName(){//生成随机数
String[] arr3=new String[]
{
"项目经理",
"项目副经理",
"项目总工",
"安全员",
"技术员",
"信息员",
"质量员",
"管理员",
};
Random ran2 = new Random();
int m = ran2.nextInt(7);
System.out.println("随机的数字是:"+m);
String c = arr3[m];
return c;
}
//判断打卡记录是否存在 员工ID 打卡日期
public static boolean judgeSign(String staffID,String signDate, ActionContext context)throws SQLException, NamingException,java.sql.SQLException {
//
Connection conn=null;//获取数据库连接
boolean is = false;
try {
conn = context.getConnection("jeecg37bzb");//获取数据库连接
java.sql.Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String strKsql = "select d.staff_id from T_sign d where d.staff_id ='"+staffID+"' and d.fSignDate = '"+signDate+"'";
System.out.println(strKsql);
//
Table table1 = DataUtils.queryData((java.sql.Connection) conn, strKsql, null, null, null, null);
// 遍历
Iterator<Row> iterator1 = table1.getRows().iterator();
if (iterator1.hasNext()) {
is = true;
}
public static boolean judgeSign(String staffID,String signDate, ActionContext context)throws SQLException, NamingException,java.sql.SQLException {
//
Connection conn=null;//获取数据库连接
boolean is = false;
try {
conn = context.getConnection("jeecg37bzb");//获取数据库连接
java.sql.Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String strKsql = "select d.staff_id from T_sign d where d.staff_id ='"+staffID+"' and d.fSignDate = '"+signDate+"'";
System.out.println(strKsql);
//
Table table1 = DataUtils.queryData((java.sql.Connection) conn, strKsql, null, null, null, null);
// 遍历
Iterator<Row> iterator1 = table1.getRows().iterator();
if (iterator1.hasNext()) {
is = true;
}
} catch (Exception e) {
// TODO: handle exception
}finally{
// TODO: handle exception
}finally{
conn.close();
}
return is;
}
}
return is;
}
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
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()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
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()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
//20180507
public static String readContentFromPost(String name,String phone) throws IOException {
// Post请求的url,与get不同的是不需要带参数
URL postUrl = new URL("https://qy.bangongyi.com/address/api/staff/add?ticket=eb1b74051b7edaaf39d4396ec49d14a9");
// 打开连接
public static String readContentFromPost(String name,String phone) throws IOException {
// Post请求的url,与get不同的是不需要带参数
URL postUrl = new URL("https://qy.bangongyi.com/address/api/staff/add?ticket=eb1b74051b7edaaf39d4396ec49d14a9");
// 打开连接
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
// 设置是否向connection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true
connection.setDoOutput(true);
// Read from the connection. Default is true.
connection.setDoInput(true);
// 默认是 GET方式
connection.setRequestMethod("POST");
// Post 请求不能使用缓存
connection.setUseCaches(false);
//设置本次连接是否自动重定向
connection.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
// 正文,正文内容其实跟get的URL中 '? '后的参数字符串一致
String uid = converterToSpell(name)+(int)((Math.random()*9+1)*10);
String content = "we_userid="+URLEncoder.encode(uid, "UTF-8")
+"&we_name="+URLEncoder.encode(name,"UTF-8")
+"&we_id=11"
+"&we_mobile="+phone;
System.out.println(content);
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
out.writeBytes(content);
//流用完记得关
out.flush();
out.close();
//获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String aa = reader.readLine();
while ((line = reader.readLine()) != null){
System.out.println("获取到的line是11111111111:"+line);
}
reader.close();
//该干的都干完了,记得把连接断了
connection.disconnect();
System.out.println("ddddddd"+aa);
return aa;
}
/**
* 获取汉字串拼音,英文字符不变
*
* @param chinese 汉字串
* @return 汉语拼音
*/
// 设置是否向connection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true
connection.setDoOutput(true);
// Read from the connection. Default is true.
connection.setDoInput(true);
// 默认是 GET方式
connection.setRequestMethod("POST");
// Post 请求不能使用缓存
connection.setUseCaches(false);
//设置本次连接是否自动重定向
connection.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
// 正文,正文内容其实跟get的URL中 '? '后的参数字符串一致
String uid = converterToSpell(name)+(int)((Math.random()*9+1)*10);
String content = "we_userid="+URLEncoder.encode(uid, "UTF-8")
+"&we_name="+URLEncoder.encode(name,"UTF-8")
+"&we_id=11"
+"&we_mobile="+phone;
System.out.println(content);
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
out.writeBytes(content);
//流用完记得关
out.flush();
out.close();
//获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String aa = reader.readLine();
while ((line = reader.readLine()) != null){
System.out.println("获取到的line是11111111111:"+line);
}
reader.close();
//该干的都干完了,记得把连接断了
connection.disconnect();
System.out.println("ddddddd"+aa);
return aa;
}
/**
* 获取汉字串拼音,英文字符不变
*
* @param chinese 汉字串
* @return 汉语拼音
*/
public static String converterToSpell(String chinese){
String pinyinName = "";
char[] nameChar = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for(int i = 0;i < nameChar.length; i++){
if (nameChar[i]>128){
try{
pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i],defaultFormat)[0];
}catch(BadHanyuPinyinOutputFormatCombination e){
e.printStackTrace();
}
}else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
public static void main(String[] args) {
//发送 GET 请求
String s=Test.sendGet("http://localhost:6144/Home/RequestString", "key=123&v=456");
System.out.println(s);
String pinyinName = "";
char[] nameChar = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for(int i = 0;i < nameChar.length; i++){
if (nameChar[i]>128){
try{
pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i],defaultFormat)[0];
}catch(BadHanyuPinyinOutputFormatCombination e){
e.printStackTrace();
}
}else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}
public static void main(String[] args) {
//发送 GET 请求
String s=Test.sendGet("http://localhost:6144/Home/RequestString", "key=123&v=456");
System.out.println(s);
//发送 POST 请求
String sr=Test.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456");
System.out.println(sr);
}
}
String sr=Test.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456");
System.out.println(sr);
}
}