private String sendRasterRequest(String target, String request, String method, String username, String password) throws HttpException, IOException { HttpClient client = new HttpClient(); EntityEnclosingMethod requestMethod = null; if (method.equalsIgnoreCase("POST")) { requestMethod = new PostMethod(target); requestMethod.setRequestHeader("Content-type", "application/xml"); } if (method.equalsIgnoreCase("PUT")) { requestMethod = new PutMethod(target); requestMethod.setRequestHeader("Content-type", "text/plain"); } requestMethod.setRequestBody(request); UsernamePasswordCredentials creds = new UsernamePasswordCredentials( username, password); client.getState().setCredentials(null, null, creds); int statusCode = client.executeMethod(requestMethod); if (!((statusCode == HttpStatus.SC_OK) || (statusCode == HttpStatus.SC_CREATED))) { System.err.println("Method failed: " + requestMethod.getStatusLine()); } // Read the response body. byte[] responseBody = requestMethod.getResponseBody(); return new String(responseBody); }
requestMethod.setRequestBody(request);
/** * Execute the PUT method for the given path. * * @param path the server relative path to put the data * @param inputStream The input stream. * @throws IOException * @return true if the method is succeeded. */ public boolean putMethod( String path, InputStream inputStream, int contentLength ) throws IOException { setClient(); PutMethod method = new PutMethod( URIUtil.encodePathQuery( path ) ); method.setFollowRedirects( super.followRedirects ); generateIfHeader( method ); if ( getGetContentType() != null && !getGetContentType().equals( "" ) ) { method.setRequestHeader( "Content-Type", getGetContentType() ); } method.setRequestContentLength( contentLength ); method.setRequestBody( inputStream ); generateTransactionHeader( method ); generateAdditionalHeaders( method ); int statusCode = client.executeMethod( method ); setStatusCode( statusCode ); return isHttpSuccess( statusCode ); }