2013-04-12 2 views
0

난 다음 사용하여 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"); 

지금 내가 보내려면

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

은 그래서 만든 다음과 같은 방법이있다 대한 요청 POST 다음과 같은 반사 : 지금

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) { 
    .... 
    } 

겠어요 -을

시스템에 프로그래밍 방식으로 로그인 할 수는 있지만 로그인 성공 후 전화를 걸 수 없습니다.

이 문제를 해결하는 데 도움을주십시오.

답변

1

서비스가 수용 할 수있는 콘텐츠의 종류에 따라 다릅니다. Xml/json/뭐라구?

리플렉션을 통해 게시해야하는 주소를 확인합니다. 해당 주소에 게시해야하는 내용은 CreateRequisitionRO 배열이 json/xml (?)로 정렬됩니다. 일단 마샬링 되었다면 요청 엔터티로 설정된 해당 내용으로 게시물 메시지를 보내면됩니다. 그런 다음 서버 측에서 요청 내용을 언 마샬하여 createRequisition() 핸들러 메소드를 호출해야합니다.

마샬링이 수행되는 방법은 프로젝트에 따라 다릅니다. JAXB가 가장 널리 퍼져있는 도서관이 많이 있습니다.

관련 문제