访问接口错误400,postman测试postUrl可以通过

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;
}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!