public void CreateDan(){
JSONObject jObj = new JSONObject();
jObj.put("ownerId", ownerId);
jObj.put("dimDepart", dimDepart);
JSONObject jObjs = new JSONObject();
jObjs.put("record",jObj);//record?
System.out.println(jObjs);
HttpURLConnection connection=urlCreate(jObjs);
String key = null;
for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) {
if (key.equalsIgnoreCase("Set-Cookie")) {
String tempCookieVal = connection.getHeaderField(i);
if (tempCookieVal.startsWith("kdservice-sessionid")) {
CookieVal = tempCookieVal;
break;
}
}
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));//400错误
String sResult=null;
String line;
boolean bResult;
StringBuffer sb = new StringBuffer("");
System.out.println(" Contents of post request ");
while ((line = reader.readLine()) != null) {
sResult = new String(line.getBytes(), "utf-8");
System.out.println(sResult);
//sb.append(line);
bResult = line.contains("\"LoginResultType\":1");
}
System.out.println(" Contents of post request ends ");
reader.close();
connection.disconnect();
//System.out.println(sb);
System.out.println(sResult);
renderJson(sResult);
}
private HttpURLConnection urlCreate(JSONObject jObjs) throws Exception {
URL postUrl = new URL(requestUrls.concat("?json="+jObjs));
System.out.println(postUrl);
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
if (CookieVal != null) {
connection.setRequestProperty("Cookie", CookieVal);
}
if (!connection.getDoOutput()) {
connection.setDoOutput(true);
}
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Authorization", access_token);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
UUID uuid = UUID.randomUUID();
int hashCode = uuid.toString().hashCode();
JSONObject jObj=new JSONObject();
out.writeBytes(jObj.toString());
out.flush();
out.close();
return connection;
}