Tuesday, 11 April 2017

Printing all the parameters of http request



Enumeration params = request.getParameterNames();

while(params.hasMoreElements()){

String paramName = (String)params.nextElement();

System.out.println("Parameter Name -> "+paramName+", Value "+request.getParameter(paramName));

}

========================================================


package com.bethecoder.tutorials.commons_httpclient;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class SimplePostRequestTest3 {

  /**
   @param args
   */
  public static void main(String[] args) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://localhost:8080/HTTP_TEST_APP/index.jsp");

    try {
      FileBody bin = new FileBody(new File("C:/ABC.txt"));
      StringBody comment = new StringBody("BETHECODER HttpClient Tutorials");

      MultipartEntity reqEntity = new MultipartEntity();
      reqEntity.addPart("fileup0", bin);
      reqEntity.addPart("fileup1", comment);
      
      reqEntity.addPart("ONE"new StringBody("11111111"));
      reqEntity.addPart("TWO"new StringBody("222222222"));
      httppost.setEntity(reqEntity);

      System.out.println("Requesting : " + httppost.getRequestLine());
      ResponseHandler<String> responseHandler = new BasicResponseHandler();
      String responseBody = httpclient.execute(httppost, responseHandler);

      System.out.println("responseBody : " + responseBody);

    catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    catch (ClientProtocolException e) {
      e.printStackTrace();
    catch (IOException e) {
      e.printStackTrace();
    finally {
      httpclient.getConnectionManager().shutdown();
    }
  }

}

==================================================================================



No comments:

Post a Comment