2016-04-12 5 views
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); 

しかし、私は次の例外取得しています:

を​​

私はここで何が欠けていますか?

答えて

1

HTTPエラーコード405の原因の1つは、ペイロードがPOSTメソッドに渡されない場合です。おそらくあなたのプログラムの場合です。

あなたは

はあなたのコードが

面白い
... 
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(); 
} 
+0

、感謝のようになります。代わりにHttpPostPostMethodを使用してペイロードを設定することができます!しかし、私はあなたが 'ペイロード 'の意味を完全に理解していません。また、 'int iResult = httpclient.executeMethod(post);'では、Eclipseは 'executeMethod'を未定義としてマークします。 – gtludwig

+1

ペイロードは、POSTまたはPUTメソッドの実行中に送信する入力データです。私はあなたの側でexecuteMethodが動作しない理由がわかりません、使用しているApacheコモンズライブラリのバージョンは何ですか? –

+0

groupId:commons-httpclient、artifactId:commons-httpclient、version:3.1 – gtludwig

関連する問題