为了账号安全,请及时绑定邮箱和手机立即绑定

Java HttpURLConnection和Apache HttpClient成功的cur

Java HttpURLConnection和Apache HttpClient成功的cur

子衿沉夜 2021-05-12 16:42:40
我正在使用Github的API更新存储库中的文件。follwing命令在curl(替换令牌字段)中起作用:curl -i -X PUT -H 'Authorization: token 2****************0' -d '{"path": "test6.txt", "message": "test", "sha":"863ba79d293acda68556bbddc1f97a29cb7b98bf","content": "dGVzdDM0Cg==", "branch": "master"}' https://api.github.com/repos/pedro-roberto/test2/contents/test6.txtsha从此处更新并获取https://api.github.com/repos/pedro-roberto/test2/contents/test6.txt 。content参数是我要用于更新的文件的base64编码(在命令行中base64 file.txt,在Java中byte[] encodedBytes = Base64.encodeBase64(JsonArray_TO_UPLOAD.toString().getBytes()); String jsonInBase64 = new String(encodedBytes);)我试图用Java发出此请求,但失败,并显示响应代码 400:Bad request尝试1Map<String, String> parameters = new HashMap<>();parameters.put("path", "test6.txt" );parameters.put("message","test");parameters.put("sha", "863ba79d293acda68556bbddc1f97a29cb7b98bf" );parameters.put("content", "dGVzdDM0Cg=="); parameters.put("branch", "master");URL url = new URL(https://api.github.com/repos/pedro-roberto/test2/contents/test6.txt);HttpURLConnection con = (HttpURLConnection) url.openConnection();con.setRequestMethod("PUT");con.setRequestProperty("Authorization","token 2********************0");con.setDoOutput(true); DataOutputStream out = new DataOutputStream(con.getOutputStream());out.writeBytes(ParameterStringBuilder.getParamsString(parameters));out.flush();out.close();
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 161 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号