qq_37619445 2018-09-13 03:00 采纳率: 0%
浏览 10147

访问接口错误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条回答 默认 最新

  • 全栈极简 博客专家认证 2018-09-13 03:22
    关注
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集