繁体   English   中英

Javax-RS-如何创建客户端以将数据推送到服务器?

[英]Javax-RS - how to create a client to push data to a server?

我需要将数据从独立客户端推送回服务器。 该客户端已经访问服务器以获取信息,对其进行解析,执行其必须执行的操作,但是现在我需要它来推回一些信息。

我尝试这样做:

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM