Package org.apache.commons.httpclient.methods

Examples of org.apache.commons.httpclient.methods.MultipartPostMethod


                            cmbURLModel.getElementAt(
                                cmbURL.getSelectedIndex()))) {
                        cmbURLModel.addElement(targetURL);
                    }

                    MultipartPostMethod filePost =
                        new MultipartPostMethod(targetURL);

                    try {
                        appendMessage("Uploading " + targetFile.getName() + " to " + targetURL);
                        filePost.addParameter(targetFile.getName(), targetFile);
                        HttpClient client = new HttpClient();
                        client.setConnectionTimeout(5000);
                        int status = client.executeMethod(filePost);
                        if (status == HttpStatus.SC_OK) {
                            appendMessage(
                                "Upload complete, response=" + filePost.getResponseBodyAsString()
                            );
                        } else {
                            appendMessage(
                                "Upload failed, response=" + HttpStatus.getStatusText(status)
                            );
                        }
                    } catch (Exception ex) {
                        appendMessage("Error: " + ex.getMessage());
                        ex.printStackTrace();
                    } finally {
                        filePost.releaseConnection();
                    }

                }
            });
View Full Code Here


        HttpMethodBase httpMethod = null;
        String useragentProperty = request.getProperty("User-Agent");
        if (formMethod.equalsIgnoreCase(FORM_MULTIPART_METHOD))
        {
            // http mutipart
            MultipartPostMethod mutlitPart = (MultipartPostMethod) (httpMethod = new MultipartPostMethod(uri));
            if (params != null && !params.isEmpty())
            {
                Iterator iter = params.entrySet().iterator();
                while (iter.hasNext())
                {
                    Map.Entry entry = (Map.Entry) iter.next();
                    String name = (String) entry.getKey();
                    String[] values = (String[]) entry.getValue();
                    if (values != null)
                        for (int i = 0, limit = values.length; i < limit; i++)
                        {
                            //System.out.println("...adding >>>POST parameter: "
                            // +name+", with value: "+values[i]+"<<<");

                            mutlitPart.addParameter(name, values[i]);
                        }
                }
            }

        }
View Full Code Here

            throw new IOException("User not logged when trying to add an event");

        ctx.recordTime();
        loadStatics(addEventStatics);

        MultipartPostMethod post = new MultipartPostMethod(addEventResultURL);
        GetMethod eventForm = new GetMethod(addEventURL);

        // TODO: Implement prepareEvent() for Rails form data
        StringBuilder buffer = new StringBuilder(256);
        post.addParameter("commit", "Create");
        // post.addParameter("controller", "events");
        post.addParameter("event[title]", RandomUtil.randomText(random, 15, 20));
        post.addParameter("event[summary]", RandomUtil.randomText(random, 50, 200));
        post.addParameter("event[description]", RandomUtil.randomText(random, 100, 495));
        post.addParameter("event[telephone]", RandomUtil.randomPhone(random, buffer));
        post.addParameter("event[event_timestamp(1i)]", "2008");
        post.addParameter("event[event_timestamp(2i)]", "10");
        post.addParameter("event[event_timestamp(3i)]", "20");
        post.addParameter("event[event_timestamp(4i)]", "20");
        post.addParameter("event[event_timestamp(5i)]", "10");

        Part imagePart = new FilePart("event_image", eventImg, "image/jpeg", null);
        Part docPart = new FilePart("event_document", eventPdf, "application/pdf", null);

        post.addPart(imagePart);
        post.addPart(docPart);
        post.addParameter("tag_list", "tag1");

        addAddress(post);

        // GET the new event form within a user session
        httpClient.executeMethod(eventForm);
        String responseBuffer = eventForm.getResponseBodyAsString();
        if (responseBuffer.length() == 0)
            throw new IOException("Received empty response");

        // Parse the authenticity_token from the response
        String token = parseAuthToken(responseBuffer);
        if (token != null)
            post.addParameter("authenticity_token", token);

        doMultiPartPost(post, "Event was successfully created.");

        ctx.recordTime();
        ++driverMetrics.addEventTotal;
View Full Code Here

        if (isLoggedOn)
            doLogout();

        ctx.recordTime(); // Start critical section
        http.readURL(addPersonURL);
        MultipartPostMethod post = new MultipartPostMethod(addPersonResultURL);
       
        // TODO: Implement preparePerson() for Rails form data
        String fields[]  = new String[8];
        StringBuilder b = new StringBuilder(256);
        int id = loadedUsers + personsAdded++ * ScaleFactors.activeUsers +
                                                        ctx.getThreadId() + 1;
        String username = UserName.getUserName(id);
        if (username == null || username.length() == 0)
            logger.warning("Username is null!");
                           
        post.addParameter("user[username]", username);
        http.readURL(checkNameURL, "name=" + username);
       
        post.addParameter("user[password]", "" + id);
        post.addParameter("user[password_confirmation]", "" + id);
        post.addParameter("user[firstname]",  RandomUtil.randomName(random, b, 2, 12).toString());
        b.setLength(0);
        post.addParameter("user[lastname]",  RandomUtil.randomName(random, b, 5, 12).toString());
        post.addParameter("user[email]", username + "@" + random.makeCString(3, 10) + ".com");
        b.setLength(0);
        post.addParameter("user[telephone]", RandomUtil.randomPhone(random, b).toString());
        post.addParameter("user[summary]", RandomUtil.randomText(random, 50, 200));
        post.addParameter("user[timezone]", RandomUtil.randomTimeZone(random));
       
        Part imagePart = new FilePart("user_image", personImg, "image/jpeg", null);
        post.addPart(imagePart);
       
        addAddress(post);

        loadStatics(addPersonStatics);
        doMultiPartPost(post, "Succeeded in creating user.");
View Full Code Here

                            cmbURLModel.getElementAt(
                                cmbURL.getSelectedIndex()))) {
                        cmbURLModel.addElement(targetURL);
                    }

                    MultipartPostMethod filePost =
                        new MultipartPostMethod(targetURL);

                    try {
                        appendMessage("Uploading " + targetFile.getName() + " to " + targetURL);
                        filePost.addParameter(targetFile.getName(), targetFile);
                        HttpClient client = new HttpClient();
                        client.setConnectionTimeout(5000);
                        int status = client.executeMethod(filePost);
                        if (status == HttpStatus.SC_OK) {
                            appendMessage(
                                "Upload complete, response=" + filePost.getResponseBodyAsString()
                            );
                        } else {
                            appendMessage(
                                "Upload failed, response=" + HttpStatus.getStatusText(status)
                            );
                        }
                    } catch (Exception ex) {
                        appendMessage("Error: " + ex.getMessage());
                        ex.printStackTrace();
                    } finally {
                        filePost.releaseConnection();
                    }

                }
            });
View Full Code Here

    /**
     * Test that the body consisting of a string part can be posted.
     */

    public void testPostStringPart() throws Exception {
        MultipartPostMethod method = new MultipartPostMethod(bodyPath);
        method.addPart(new StringPart("param", "Hello", "ISO-8859-1"));

        httpClient.executeMethod(method);

        assertEquals(200,method.getStatusCode());
        String body = method.getResponseBodyAsString();
        assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param\"") >= 0);
        assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
        assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
        assertTrue(body.indexOf("Hello") >= 0);
    }
View Full Code Here

    /**
     * Test that the body consisting of a file part can be posted.
     */
    public void testPostFilePart() throws Exception {
        MultipartPostMethod method = new MultipartPostMethod(bodyPath);
        byte[] content = "Hello".getBytes();
        method.addPart(
          new FilePart(
            "param1",
            new ByteArrayPartSource("filename.txt", content),
            "text/plain",
            "ISO-8859-1"));

        httpClient.executeMethod(method);

        assertEquals(200,method.getStatusCode());
        String body = method.getResponseBodyAsString();
        assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
        assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
        assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
        assertTrue(body.indexOf("Hello") >= 0);
    }
View Full Code Here

        HttpMethodBase httpMethod = null;
        String useragentProperty = request.getProperty("User-Agent");
        if (formMethod.equalsIgnoreCase(FORM_MULTIPART_METHOD))
        {
            // http mutipart
            MultipartPostMethod mutlitPart = (MultipartPostMethod) (httpMethod = new MultipartPostMethod(uri));
            if (params != null && !params.isEmpty())
            {
                Iterator iter = params.entrySet().iterator();
                while (iter.hasNext())
                {
                    Map.Entry entry = (Map.Entry) iter.next();
                    String name = (String) entry.getKey();
                    String[] values = (String[]) entry.getValue();
                    if (values != null)
                        for (int i = 0, limit = values.length; i < limit; i++)
                        {
                            //System.out.println("...adding >>>POST parameter: "
                            // +name+", with value: "+values[i]+"<<<");

                            mutlitPart.addParameter(name, values[i]);
                        }
                }
            }

        }
View Full Code Here

        ToolConfig config = getToolConfig(toolID);
       
        String response;
        try {
            HttpClient client = new HttpClient();
            MultipartPostMethod mPost = new MultipartPostMethod(config.getTool().getExecutablePath());
            client.setConnectionTimeout(8000);


            // MultipartPostMethod needs a file instance
            File sample = File.createTempFile(samplename+System.nanoTime(), "tmp");
            OutputStream out = new BufferedOutputStream(new FileOutputStream(sample));
            out.write(data);
            out.close();
           
            mPost.addParameter("datei", samplename, sample);
           
            int statusCode = client.executeMethod(mPost);
           
            response = mPost.getResponseBodyAsString();
           
            return response+ config.getParams();

        } catch (HttpException e) {
            throw new PlatoServiceException("Could not connect to GRATE.", e);
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.methods.MultipartPostMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.