HTTP 错误 400。请求的 header 名称无效
HTTP Error 400. The request has an invalid header name
我有一个包含以下代码的 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 是一个加密输入字符串和 returns 密码的函数。但是,当我 运行 这段代码时,出现以下错误,其中包含语句 client.execute:
HTTP Error 400. The request has an invalid header name
但是,如果我 hard-code 下面 header 使用我使用相同的 DoAESEncrypt 函数生成的加密密码,它工作正常:
post.addHeader("auth","5PE7vNMYVFJT/tgYo7TGuPO05lqMQx5w3BAJKDS567A73vM0TYFmWO5tP=");
出于某种原因,DoAESEncrypt 函数在密文中添加了回车 return“\r\n”。这已被删除并且工作正常。
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 应用程序。我正在尝试使用 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 是一个加密输入字符串和 returns 密码的函数。但是,当我 运行 这段代码时,出现以下错误,其中包含语句 client.execute:
HTTP Error 400. The request has an invalid header name
但是,如果我 hard-code 下面 header 使用我使用相同的 DoAESEncrypt 函数生成的加密密码,它工作正常:
post.addHeader("auth","5PE7vNMYVFJT/tgYo7TGuPO05lqMQx5w3BAJKDS567A73vM0TYFmWO5tP=");
出于某种原因,DoAESEncrypt 函数在密文中添加了回车 return“\r\n”。这已被删除并且工作正常。
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","")