gpt4 book ai didi

java - HTTP 错误 400。请求的 header 名称无效

转载 作者:行者123 更新时间:2023-11-30 06:52:54 25 4
gpt4 key购买 nike

我有一个包含以下代码的 Java 应用程序。我正在尝试使用 HttpClient POST 调用 REST Web 服务。我需要发送自定义 header “auth”进行身份验证。 header 的值是由“:”分隔的加密凭据。

        String strAuth = DoAESEncrypt("userid:pwd");
String strContent = DoAESEncrypt("{\"Name\":Tester,\"Id\":\"123\",\"NickName\":null,\"IsLocked\":false}");

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://domainname/servicename");
post.addHeader("auth",strAuth);
post.addHeader("Content-Type", "application/json");
StringEntity input = new StringEntity(strContent);
post.setEntity(input);
HttpResponse response = client.execute(post);

DoAESEncrypt 是一个加密输入字符串并返回 Cipher 的函数。但是,当我运行此代码时,出现以下错误,其中包含语句 client.execute:

HTTP Error 400. The request has an invalid header name

但是,如果我使用相同的 DoAESEncrypt 函数生成的加密密码对下面的 header 进行硬编码,它就可以正常工作:

      post.addHeader("auth","5PE7vNMYVFJT/tgYo7TGuPO05lqMQx5w3BAJKDS567A73vM0TYFmWO5tP=");

最佳答案

String strAuth = DoAESEncrypt("userid:pwd");
String strContent = DoAESEncrypt("{\"Name\":Tester,\"Id\":\"123\",\"NickName\":null,\"IsLocked\":false}");

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://domainname/servicename");
post.addHeader("auth",strAuth.replace("\n","").replace("\r","");
post.addHeader("Content-Type", "application/json");
StringEntity input = new StringEntity(strContent);
post.setEntity(input);
HttpResponse response = client.execute(post);

Replace all \n and \r from your encrypted string which you passing to header as follow

strAuth.replace("\n","").replace("\r","")

关于java - HTTP 错误 400。请求的 header 名称无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38174460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com