Wednesday, February 22, 2012

Sending Http request uisng HttpClient

 try {
            File file = new File(fileName);
            Part[] parts = {
                new FilePart(fileName, file)
                };

            PersistenceService salesForceService = new SalesForceService();
            PartnerConnection connection = salesForceService.authenticate(authEndpoint, username, password);
            String serviceEndpoint = connection.getConfig().getServiceEndpoint();
            PostMethod postMethod = new PostMethod(uri);
            Header header1 = new Header("username", username);
            Header header2 = new Header("password", password);
            Header header3 = new Header("sessionId", connection.getSessionHeader().getSessionId());
            Header header4 = new Header("serviceEndpoint", serviceEndpoint);
           
            postMethod.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
            postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
           
            postMethod.addRequestHeader(header1);
            postMethod.addRequestHeader(header2);
            postMethod.addRequestHeader(header3);
            postMethod.addRequestHeader(header4);
           

            System.out.println(connection.getSessionHeader().getSessionId());
            System.out.println(header3.getValue());
           
            HttpClient client = new HttpClient();
            client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
           
            int status = client.executeMethod(postMethod); //run

            System.out.println(HttpStatus.getStatusText(status));

        } catch (Exception e) {
            e.printStackTrace();
        }

No comments:

Post a Comment