Java Questions
Java Questions

Reputation: 7953

how to send the url post using httpclient

i login to my project like the following using httpclient :

public void login(String username,String password){
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://localhost:8080/j_spring_security_check");
        try {
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
          nameValuePairs.add(new BasicNameValuePair("j_username", username));
          nameValuePairs.add(new BasicNameValuePair("j_password", password));

          post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
          HttpResponse response = client.execute(post);

          BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

and i use the above like the following :

HttpClientRequests httpRequest = new HttpClientRequests();
httpRequest.login("mayank","hexgen");

now i want to send a POST request for method which is there like the following :

@RequestMapping(method = RequestMethod.POST, value = "/trade/createrequisition")
    public @ResponseBody
    void createRequisition(@RequestBody CreateRequisitionRO[] request,
            @RequestHeader("validateOnly") boolean validateOnly) {
....
}

so i have created reflection like the following :

HttpClientRequests httpRequest = new HttpClientRequests();
        Class[] paramString = new Class[1]; 
        paramString[0] = String.class;

        Class parames = CreateRequisitionRO[].class;

        CreateRequisitionRO[] roRqequest = new CreateRequisitionRO[1];
        boolean istrueOrFalse=true;


        Class booleanVal ;  
        booleanVal = Boolean.TYPE;

        Class cls;
        try {
            cls = Class.forName("com.hexgen.api.facade.HexgenWebAPI");

            Method method = cls.getDeclaredMethod("createRequisition", parames,booleanVal);

            RequestMapping methodRequestMappingAnnotation = method.getAnnotation(RequestMapping.class);
            RequestMethod[] methods = methodRequestMappingAnnotation.method();
            String[] mappingValues = methodRequestMappingAnnotation.value();
            //String methodType = methods[0].name();
            //String url = mappingValues[0];

            httpRequest.login("mayank","hexgen");

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

now after this httpRequest.login("mayank","hexgen"); how to send request to access the following method :

@RequestMapping(method = RequestMethod.POST, value = "/trade/createrequisition")
        public @ResponseBody
        void createRequisition(@RequestBody CreateRequisitionRO[] request,
                @RequestHeader("validateOnly") boolean validateOnly) {
    ....
    }

So.

I am able to login to the system programatically but not able to call after successful login.

Please help me to resolve this.

Upvotes: 0

Views: 833

Answers (1)

zagyi
zagyi

Reputation: 17518

It depends on what kind of content the service can accept. Xml/json/whatever?

I see you get the address where you have to post via reflection. The content you should post to that address is a CreateRequisitionRO array marshalled to json/xml(?). Once you have them marshalled, simply send the post message with that conent set as request entity. The server side should then unmarshall the request content and invoke the createRequisition() handler method.

How the marshalling is done depends on your project. There are many libraries for that, JAXB being the most prevalent, I guess.

Upvotes: 1

Related Questions