java.lang.IllegalStateException:设置正文参数时,名称为空
下面是一个向某个系统发出POST请求的示例 请求如下所示: 我尝试用以下方法实现它:java.lang.IllegalStateException:设置正文参数时,名称为空,java,rest,http,post,apache-httpclient-4.x,Java,Rest,Http,Post,Apache Httpclient 4.x,下面是一个向某个系统发出POST请求的示例 请求如下所示: 我尝试用以下方法实现它: /** * This method creates a HTTP POST/PUT/PATCH request and fills body * with data from Map * @param urlToPrepareRequest URL for a request * @param keyValueToPutInBodyRequest Map of enti
/**
* This method creates a HTTP POST/PUT/PATCH request and fills body
* with data from Map
* @param urlToPrepareRequest URL for a request
* @param keyValueToPutInBodyRequest Map of entities to put in body request
* @param httpMethod HTTP method (POST, PUT, PATCH are available)
* @throws IllegalArgumentException if httpMethod is wrong
* @return ready HTTP POST/PUT/PATCH with filled body
*/
public static HttpEntityEnclosingRequestBase createHttpRequestAndFillBody(String urlToPrepareRequest, Map<String, Object> keyValueToPutInBodyRequest, String httpMethod)
{
HttpEntityEnclosingRequestBase httpRequest;
if (httpMethod.equals("POST"))
{
httpRequest = new HttpPost(urlToPrepareRequest);
}
else if (httpMethod.equals("PUT"))
{
httpRequest = new HttpPut(urlToPrepareRequest);
}
else if (httpMethod.equals("PATCH"))
{
httpRequest = new HttpPatch(urlToPrepareRequest);
}
else throw new IllegalArgumentException("HTTP method can't be used " + httpMethod + " available values are \"POST\", \"PUT\", \"PATCH\"");
Iterator it = keyValueToPutInBodyRequest.entrySet().iterator();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
while (it.hasNext())
{
Map.Entry pair = (Map.Entry)it.next();
String keyOfBodyToPut = (String) pair.getKey();
Object valueOfBodyToPut = pair.getValue();
if(!(valueOfBodyToPut instanceof byte[]))
{
builder.addTextBody(keyOfBodyToPut, valueOfBodyToPut.toString());
}
else
{
builder.addBinaryBody(keyOfBodyToPut, (byte[]) valueOfBodyToPut);
}
}
HttpEntity entity = builder.build();
httpRequest.setEntity(entity);
return httpRequest;
}
这都是因为在地图中有以下代码:
bodyParams.put("", currentChunk);
currentChunk
的类型为byte[]
。起初,我认为可以在body请求中放置一个空键(因为在上面的屏幕上没有键),但现在我认为可能不是
有什么问题吗
我如何使我的方法更符合邮递员的请求
提前谢谢。我有确切的错误消息。你发现问题了吗?事实证明,你不能在没有名字的情况下设置body参数。我使用wireshark跟踪了发送到服务器的包。我的解决方案是将名称设置为“file”。请尝试使用wireshark,如果需要帮助,请发短信给我。我有确切的错误消息。你发现问题了吗?事实证明,你不能在没有名字的情况下设置body参数。我使用wireshark跟踪了发送到服务器的包。我的解决方案是将名称设置为“file”。如果需要帮助,请尝试使用wireshark或发短信给我
bodyParams.put("", currentChunk);