本文整理汇总了Java中org.apache.commons.httpclient.methods.PostMethod类的典型用法代码示例。如果您正苦于以下问题:Java PostMethod类的具体用法?Java PostMethod怎么用?Java PostMethod使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PostMethod类属于org.apache.commons.httpclient.methods包,在下文中一共展示了PostMethod类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: httpClientPost
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
public static final String httpClientPost(String url, ArrayList<NameValuePair> list) {
String result = "";
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
try {
NameValuePair[] params = new NameValuePair[list.size()];
for (int i = 0; i < list.size(); i++) {
params[i] = list.get(i);
}
postMethod.addParameters(params);
client.executeMethod(postMethod);
result = postMethod.getResponseBodyAsString();
} catch (Exception e) {
logger.error("", e);
} finally {
postMethod.releaseConnection();
}
return result;
}
示例2: post
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
@Override
public HttpResponse post(URL urlObj, byte[] payload, String userName, String password,
int timeout) {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(urlObj.toString());
method.setRequestEntity(new ByteArrayRequestEntity(payload));
method.setRequestHeader("Content-type", "application/json");
client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
client.getParams().setSoTimeout(1000 * timeout);
client.getParams().setConnectionManagerTimeout(1000 * timeout);
if (userName != null && password != null) {
setBasicAuthorization(method, userName, password);
}
try {
int response = client.executeMethod(method);
return new HttpResponse(response, method.getResponseBody());
} catch (IOException e) {
throw new RuntimeException("Failed to process post request URL: " + urlObj, e);
} finally {
method.releaseConnection();
}
}
示例3: testPostProxyAuthHostAuthConnClose
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
/**
* Tests POST via authenticating proxy + host auth + connection close
*/
public void testPostProxyAuthHostAuthConnClose() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
this.client.getState().setCredentials(AuthScope.ANY, creds);
this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
this.server.setRequestHandler(handlerchain);
this.proxy.requireAuthentication(creds, "test", true);
PostMethod post = new PostMethod("/");
post.setRequestEntity(new StringRequestEntity("Like tons of stuff", null, null));
try {
this.client.executeMethod(post);
assertEquals(HttpStatus.SC_OK, post.getStatusCode());
assertNotNull(post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}
}
示例4: chcekConnectionStatus
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
public void chcekConnectionStatus() throws IOException {
HttpClient httpClient = new HttpClient();
//TODO : add connection details while testing only,remove it once done
String teradatajson = "{\"username\":\"\",\"password\":\"\",\"hostname\":\"\",\"database\":\"\",\"dbtype\":\"\",\"port\":\"\"}";
PostMethod postMethod = new PostMethod("http://" + HOST_NAME + ":" + PORT + "/getConnectionStatus");
//postMethod.addParameter("request_parameters", redshiftjson);
postMethod.addParameter("request_parameters", teradatajson);
int response = httpClient.executeMethod(postMethod);
InputStream inputStream = postMethod.getResponseBodyAsStream();
byte[] buffer = new byte[1024 * 1024 * 5];
String path = null;
int length;
while ((length = inputStream.read(buffer)) > 0) {
path = new String(buffer);
}
System.out.println("Response of service: " + path);
System.out.println("==================");
}
示例5: calltoReadService
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
public void calltoReadService() throws IOException {
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod("http://" + HOST_NAME + ":" + PORT + "/read");
postMethod.addParameter("jobId", JOB_ID);
postMethod.addParameter("componentId", COMPONENT_ID);
postMethod.addParameter("socketId", SOCKET_ID);
postMethod.addParameter("basePath", BASE_PATH);
postMethod.addParameter("userId", USER_ID);
postMethod.addParameter("password", PASSWORD);
postMethod.addParameter("file_size", FILE_SIZE_TO_READ);
postMethod.addParameter("host_name", HOST_NAME);
InputStream inputStream = postMethod.getResponseBodyAsStream();
byte[] buffer = new byte[1024 * 1024 * 5];
String path = null;
int length;
while ((length = inputStream.read(buffer)) > 0) {
path = new String(buffer);
}
System.out.println("response of service: " + path);
}
示例6: httpClientPost
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
public static final String httpClientPost(String url, ArrayList<NameValuePair> list) {
String result = "";
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
try {
NameValuePair[] params = new NameValuePair[list.size()];
for (int i = 0; i < list.size(); i++) {
params[i] = list.get(i);
}
postMethod.addParameters(params);
client.executeMethod(postMethod);
result = postMethod.getResponseBodyAsString();
} catch (Exception e) {
logger.error(e);
} finally {
postMethod.releaseConnection();
}
return result;
}
示例7: testPostHostAuthConnKeepAlive
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
/**
* Tests POST via non-authenticating proxy + host auth + connection keep-alive
*/
public void testPostHostAuthConnKeepAlive() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
this.client.getState().setCredentials(AuthScope.ANY, creds);
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
this.server.setRequestHandler(handlerchain);
PostMethod post = new PostMethod("/");
post.setRequestEntity(new StringRequestEntity("Like tons of stuff", null, null));
try {
this.client.executeMethod(post);
assertEquals(HttpStatus.SC_OK, post.getStatusCode());
assertNotNull(post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}
}
示例8: testEnclosedEntityNegativeLengthHTTP1_0
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
public void testEnclosedEntityNegativeLengthHTTP1_0() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(
instream, -14);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
method.setContentChunked(false);
method.getParams().setVersion(HttpVersion.HTTP_1_0);
this.server.setHttpService(new EchoService());
try {
this.client.executeMethod(method);
fail("ProtocolException should have been thrown");
} catch (ProtocolException ex) {
// expected
} finally {
method.releaseConnection();
}
}
示例9: buildPayRequest
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
/**
* Creates a pay request from the buyer (who is indirectly referenced by the
* preapproval key) to the seller/receiver.
*
* @param preapprovalKey
* The key from a former preapproval request
* @param amount
* amount to be transfered in Euro
* @return
* @throws Exception
*/
public PostMethod buildPayRequest(String preapprovalKey, String receiver,
String amount) throws Exception {
final Document doc = createBody("PayRequest");
final Element payReq = doc.getDocumentElement();
addElement(payReq, "actionType", "PAY");
addElement(payReq, "preapprovalKey", preapprovalKey);
addElement(payReq, "senderEmail",
"[email protected]");
final Element receiverList = addElement(payReq, "receiverList", null);
final Element receiverElement = addElement(receiverList, "receiver",
null);
addElement(receiverElement, "amount", amount);
addElement(receiverElement, "email", receiver);
return createPostMethod(doc, "Pay");
}
示例10: doPost
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
/**
* Sends the request to paypal. Input is preapprovalKey of buyer, email of
* seller, and amount to be transferred.
*
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
final PaypalRequest paypalRequest = new PaypalRequest(
request.getRemoteAddr());
final PostMethod post = paypalRequest.buildPayRequest(
request.getParameter("preapprovalKey"),
request.getParameter("email"),
request.getParameter("amount"));
setDefaultAttributes(request, paypalRequest,
sendPaypalRequest(post));
request.getRequestDispatcher("/payResponse.jsp").forward(request,
response);
} catch (Exception e) {
e.printStackTrace();
}
}
示例11: testPostProxyAuthHostAuthConnKeepAlive
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
/**
* Tests POST via authenticating proxy + host auth + connection keep-alive
*/
public void testPostProxyAuthHostAuthConnKeepAlive() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
this.client.getState().setCredentials(AuthScope.ANY, creds);
this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
this.server.setRequestHandler(handlerchain);
this.proxy.requireAuthentication(creds, "test", true);
PostMethod post = new PostMethod("/");
post.setRequestEntity(new StringRequestEntity("Like tons of stuff", null, null));
try {
this.client.executeMethod(post);
assertEquals(HttpStatus.SC_OK, post.getStatusCode());
assertNotNull(post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}
}
示例12: toString
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
/**
* Help method for log
*
* @param method method
*/
protected void toString(HttpMethod method) {
logger.info("===============HTTP METHOD===============");
final String path = method.getPath();
logger.info("path = " + path);
final Header[] headers = method.getRequestHeaders();
StringBuilder builder = new StringBuilder();
for (Header header : headers) {
if (header != null)
builder.append(header.toString());
}
logger.info("header = \n" + builder.toString().trim());
if (method instanceof PostMethod) {
PostMethod postMethod = (PostMethod) method;
builder = new StringBuilder();
final NameValuePair[] parameters = postMethod.getParameters();
for (NameValuePair pair : parameters) {
builder.append(pair.getName()).append("=").append(pair.getValue()).append("\n");
}
}
logger.info("post parameters: \n" + builder.toString().trim());
logger.info("query string = " + method.getQueryString());
logger.info("=========================================");
}
示例13: createPostMethod
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
private PostMethod createPostMethod(KalturaParams kparams,
KalturaFiles kfiles, String url) {
PostMethod method = new PostMethod(url);
method.setRequestHeader("Accept","text/xml,application/xml,*/*");
method.setRequestHeader("Accept-Charset","utf-8,ISO-8859-1;q=0.7,*;q=0.5");
if (!kfiles.isEmpty()) {
method = this.getPostMultiPartWithFiles(method, kparams, kfiles);
} else {
method = this.addParams(method, kparams);
}
if (isAcceptGzipEncoding()) {
method.addRequestHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
}
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler (3, false));
return method;
}
示例14: testEnclosedEntityChunkedHTTP1_0
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
public void testEnclosedEntityChunkedHTTP1_0() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(
instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
method.setContentChunked(true);
method.getParams().setVersion(HttpVersion.HTTP_1_0);
this.server.setHttpService(new EchoService());
try {
this.client.executeMethod(method);
fail("ProtocolException should have been thrown");
} catch (ProtocolException ex) {
// expected
} finally {
method.releaseConnection();
}
}
示例15: getDebugFileMethod
import org.apache.commons.httpclient.methods.PostMethod; //导入依赖的package包/类
/**
*
* Get post method to get csv debug file
*
* @param jobDetails
* @param fileSize
* @return {@link PostMethod}
* @throws NumberFormatException
* @throws MalformedURLException
*/
public PostMethod getDebugFileMethod(JobDetails jobDetails,String fileSize) throws NumberFormatException, MalformedURLException {
URL url = new URL(POST_PROTOCOL, getHost(jobDetails), getPortNo(jobDetails), DebugServiceMethods.GET_DEBUG_FILE_PATH);
PostMethod postMethod = new PostMethod(url.toString());
postMethod.addParameter(DebugServicePostParameters.JOB_ID, jobDetails.getUniqueJobID());
postMethod.addParameter(DebugServicePostParameters.COMPONENT_ID, jobDetails.getComponentID());
postMethod.addParameter(DebugServicePostParameters.SOCKET_ID, jobDetails.getComponentSocketID());
postMethod.addParameter(DebugServicePostParameters.BASE_PATH, jobDetails.getBasepath());
postMethod.addParameter(DebugServicePostParameters.USER_ID, jobDetails.getUsername());
postMethod.addParameter(DebugServicePostParameters.DEBUG_SERVICE_PWD, jobDetails.getPassword());
postMethod.addParameter(DebugServicePostParameters.FILE_SIZE, fileSize);
postMethod.addParameter(DebugServicePostParameters.HOST_NAME, getHost(jobDetails));
LOGGER.debug("Calling debug service to get csv debug file from url :: "+url);
return postMethod;
}