0
スタンドアロンクライアントからサーバーにデータをプッシュバックする必要があります。このクライアントはすでにサーバーにアクセスして情報を取得し、解析し、実行する必要がある情報を実行しますが、情報をプッシュバックする必要があります。Javax-RS - サーバーにデータをプッシュするクライアントを作成する方法は?
私はこれやって試してみました:
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);
しかし、私は次の例外取得しています:
を私はここで何が欠けていますか?
、感謝のようになります。代わりに
HttpPost
のPostMethod
を使用してペイロードを設定することができます!しかし、私はあなたが 'ペイロード 'の意味を完全に理解していません。また、 'int iResult = httpclient.executeMethod(post);'では、Eclipseは 'executeMethod'を未定義としてマークします。 – gtludwigペイロードは、POSTまたはPUTメソッドの実行中に送信する入力データです。私はあなたの側でexecuteMethodが動作しない理由がわかりません、使用しているApacheコモンズライブラリのバージョンは何ですか? –
groupId:commons-httpclient、artifactId:commons-httpclient、version:3.1 – gtludwig