我使用 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) {
....
}
所以。
我能够以编程方式登录系统,但成功登录后无法调用。
请帮我解决这个问题。