作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将数据从独立客户端推送回服务器。该客户端已经访问服务器以获取信息、解析它、执行它必须执行的操作,但现在我需要它推送一些信息。
我尝试过这样做:
public class UpdateServer {
public void update(int resultSetRowCount) {
AgentConfiguration config = new AgentConfiguration();
config.init();
String agentId = AgentConfiguration.agent_id;
String serverIp = AgentConfiguration.server_ip;
String serverPort = AgentConfiguration.server_port;
Calendar cal = Calendar.getInstance();
StringBuilder timestamp = new StringBuilder();
timestamp.append(cal.get(Calendar.YEAR));
timestamp.append(String.format("%02d",cal.get(Calendar.MONTH) + 1));
timestamp.append(String.format("%02d",cal.get(Calendar.DAY_OF_MONTH)));
timestamp.append(String.format("%02d",cal.get(Calendar.HOUR_OF_DAY)));
timestamp.append(String.format("%02d",cal.get(Calendar.MINUTE)));
timestamp.append(String.format("%02d",cal.get(Calendar.SECOND)));
String date = timestamp.toString();
String uri = "http://" + serverIp + ":" + serverPort + "/hrm/alerts/" + agentId + "/" + date + "/" + resultSetRowCount;
System.out.println(uri);
try {
// create HTTP Client
HttpClient httpClient = HttpClientBuilder.create().build();
// Create new patchRequest with below mentioned URL
HttpPost postRequest = new HttpPost(uri);
// Add additional header to postRequest which accepts application/xml data
postRequest.addHeader("accept", "application/xml");
// Execute your request and catch response
HttpResponse response = httpClient.execute(postRequest);
// Check for HTTP response code: 200 = success
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我正在尝试使用格式化的 uri 访问服务器:
new UpdateServer().update(resultSetRowCount);
但我遇到以下异常:
Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 405
我在这里缺少什么?
最佳答案
http 错误代码 405 的原因之一是未将有效负载传递给 POST 方法。您的程序可能就是这种情况。
您可以使用PostMethod
代替HttpPost
并设置有效负载
你的代码看起来像
...
String uri = "http://" + serverIp + ":" + serverPort + "/hrm/alerts/" + agentId + "/" + date + "/" + resultSetRowCount;
System.out.println(uri);
try
{
// create HTTP Client
HttpClient httpClient = HttpClientBuilder.create().build();
PostMethod post = new PostMethod(uri);
RequestEntity requestEntity = new StringRequestEntity(payload, "application/xml", null/*charset default*/);
post.setRequestEntity(requestEntity);
int iResult = httpclient.executeMethod(post);
// get the status code using post.getStatusCode()
// you can get the response using post.getResponseBodyAsString()
// Check for HTTP response code: 200 = success
if (post.getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + post.getStatusCode());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
关于Javax-RS - 如何创建客户端将数据推送到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36576439/
我是一名优秀的程序员,十分优秀!