簡體   English   中英

如何使用httpclient發送URL帖子

[英]how to send the url post using httpclient

我使用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();
        }
      }

我使用上面的類似以下內容:

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

現在我想發送POST請求,請求方法如下:

@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數組。 將它們編組后,只需發送該郵件集作為請求實體的帖子即可。 然后,服務器端應解組請求內容,並調用createRequisition()處理程序方法。

編組的完成方式取決於您的項目。 為此,有很多庫,我猜JAXB是最流行的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM