Java 如何使用httpclient发送url帖子

Java 如何使用httpclient发送url帖子,java,spring-mvc,reflection,apache-httpclient-4.x,Java,Spring Mvc,Reflection,Apache Httpclient 4.x,我使用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<

我使用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();
        }
      }
现在我想发送一个
POST
方法请求,如下所示:

HttpClientRequests httpRequest = new HttpClientRequests();
httpRequest.login("mayank","hexgen");
@RequestMapping(method = RequestMethod.POST, value = "/trade/createrequisition")
    public @ResponseBody
    void createRequisition(@RequestBody CreateRequisitionRO[] request,
            @RequestHeader("validateOnly") boolean validateOnly) {
....
}
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();
        } 
因此,我创建了如下反射:

HttpClientRequests httpRequest = new HttpClientRequests();
httpRequest.login("mayank","hexgen");
@RequestMapping(method = RequestMethod.POST, value = "/trade/createrequisition")
    public @ResponseBody
    void createRequisition(@RequestBody CreateRequisitionRO[] request,
            @RequestHeader("validateOnly") boolean validateOnly) {
....
}
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();
        } 
现在在这个
httpRequest.login(“mayank”,“hexgen”)如何发送请求以访问以下方法:

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

我可以通过编程方式登录系统,但在成功登录后无法调用


请帮我解决这个问题。

这取决于服务可以接受什么类型的内容。Xml/json/随便什么

我看到你通过思考得到了你必须发布的地址。您应该发布到该地址的内容是编组为json/xml(?)的
CreateRequisitionRO
数组。一旦对它们进行编组,只需将该内容集作为请求实体发送post消息。然后,服务器端应解组请求内容并调用
createrequision()
handler方法

编组的方式取决于您的项目。有很多这样的库,我想JAXB是最流行的