public String coolPutCherryPyHttpClient() throws IOException, FileNotFoundException{
String res = "none";
HttpClient client = new HttpClient();
PutMethod put = new PutMethod(this.writenodeUrl);
try {
String payloaddata = " 999 999. "
+ "";
client.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(this.name, this.psswd);
client.getState().setCredentials(new AuthScope(serverUrl, portNumber, AuthScope.ANY_REALM), defaultcreds);
NameValuePair par = new NameValuePair();
par.setName("payload");
par.setValue(payloaddata);
NameValuePair[] params = new NameValuePair[1];
params[0] = par;
put.setQueryString(params);
// Execute the method.
int statusCode = client.executeMethod(put);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + put.getStatusLine());
}
// Read the response body.
byte[] responseBody = put.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
res = new String(responseBody);
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
put.releaseConnection();
}
return res;
}
No comments:
Post a Comment