使用数据将POST发送到servlet

时间:2013-02-20 15:37:26

标签: java servlets testng smartgwt smartclient

我已成功使用cURL向使用GWT / SmartGWT和SmartClient以Java编写的Web应用程序提交POST请求。 cURL命令如下:

`curl -v --header "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" --header "CUSTOM-userid: ghettosamson-cURL1" -D respheaders1.txt -o response-1.txt --data _transaction=%3Ctransaction%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2F10%2FXMLSchema-instance%22%20xsi%3Atype%3D%22xsd%3AObject%22%3E%3CtransactionNum%20xsi%3Atype%3D%22xsd%3Along%22%3E16%3C%2FtransactionNum%3E%3Coperations%20xsi%3Atype%3D%22xsd%3AList%22%3E%3Celem%20xsi%3Atype%3D%22xsd%3AObject%22%3E%3CappID%3Emyapplication%3C%2FappID%3E%3CclassName%3EmyServlet%3C%2FclassName%3E%3CmethodName%3EmyMethodName%3C%2FmethodName%3E%3Carguments%20xsi%3Atype%3D%22xsd%3AList%22%3E%3Celem%3EmyDataSource%3C%2Felem%3E%3Celem%3EBIRTTemplate.rptdesign%3C%2Felem%3E%3Celem%3Ereporttype%3C%2Felem%3E%3Celem%3Ereporttype%20Data%20Set%3C%2Felem%3E%3Celem%3E1%3C%2Felem%3E%3Celem%3E%3C%2Felem%3E%3Celem%3Eghettosamson%20cURL%20Report%3C%2Felem%3E%3Celem%3ESTART_TIMESTAMP%20IS%20NOT%20NULL%20AND%20\(START_TIMESTAMP%20%26lt%3B%3D%20TO_DATE\(%26apos%3B2013-01-04%2023%3A59%3A00%26apos%3B%2C%26apos%3BYYYY-MM-DD%20HH24%3AMI%3ASS%26apos%3B\)%20OR%20START_TIMESTAMP%20IS%20NULL\)\)\)\)%0A%20%20%20%20%09%09%3C%2Felem%3E%3Celem%3E%3C%2Felem%3E%3C%2Farguments%3E%3Cis_ISC_RPC_DMI%20xsi%3Atype%3D%22xsd%3Aboolean%22%3Etrue%3C%2Fis_ISC_RPC_DMI%3E%3C%2Felem%3E%3C%2Foperations%3E%3C%2Ftransaction%3E --data isc_tnum=10 --data protocolVersion=1.0 myipaddress:myport/myapplication/myapplication/sc/IDACall?isc_rpc=1&isc_v=v8.2p_2012-08-28&isc_xhr=1 &`

收到的回复写入文件,如下:

`<HTML>
<BODY ONLOAD='var results = document.formResults.results.value;null'><BR>
<BR><BR><BR>  <BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
<FORM name='formResults'><TEXTAREA readonly name='results'>
//isc_RPCResponseStart-->[{status:0,data:"http://myipaddress:myport/myapplication
/reports/ghettosamson-cURL1.pdf"}]//isc_RPCResponseEnd</TEXTAREA></FORM>
</BODY></HTML>`

我正在尝试使用java复制此请求,并将其作为testng测试运行。我从这里尝试了几个不同的例子,没有任何作用。以下是我尝试的代码示例:

`@Autowired @Qualifier("contentHeader")
 private Header theContentHeader; 
 @Autowired @Qualifier("userHeader")
 private Header theUserHeader;   
 @Autowired @Qualifier("transactionPair")
 private NameValuePair theTransactionPair;
 @Autowired @Qualifier("iscPair")
 private NameValuePair theIscPair;   
 @Autowired @Qualifier("protocolPair")
 private NameValuePair theProtocolPair;
 @Test
 public void testConcurrentCreateAndOpenReportRequests() 
 throws ClientProtocolException, IOException
 {
    Assert.notNull(theTestPost);
    Assert.notNull(theContentHeader);
    Assert.notNull(theUserHeader);
    Assert.notNull(theTransactionPair);      
    HttpClient httpclient = new DefaultHttpClient();
    theTestPost.addHeader(theContentHeader);
    theTestPost.addHeader(theUserHeader);      
    ArrayList<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
    parameters.add(theTransactionPair);
    parameters.add(new BasicNameValuePair("isc_tnum", "10"));
    parameters.add(new BasicNameValuePair("protocolVersion", "1.0"));
    theTestPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
    HttpResponse response = httpclient.execute(theTestPost);
    HttpEntity entity = response.getEntity();      
    Assert.notNull(entity);
    InputStream instream = entity.getContent();
    System.out.println(instream.toString());
    instream.close();      
 }
 @Test
 public void testSimplePost() throws HttpException, IOException
 {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod("http://myipaddress:myport/
    myapplication/myapplication/sc/IDACall?isc_rpc=1&isc_v=v8.2p_2012-08-28&
    isc_xhr=1");
    method.addRequestHeader(theContentHeader);
    method.addRequestHeader(theUserHeader);
    method.addParameter(theTransactionPair);
    method.addParameter("isc_tnum", "10");
    method.addParameter("protocolVersion", "1.0");
    int statuscode = client.executeMethod(method);
    if (statuscode != -1)
    {
       InputStream instream = method.getResponseBodyAsStream(); 
       System.out.println(instream);
    }
 }   
 @Test
 public void testAnomalyPost() throws IOException
 {
    String urlParameters = String.format("%s=%s&%s=%s&%s=%s", 
                                         theTransactionPair.getName(),
                                         theTransactionPair.getValue(),
                                         theIscPair.getName(),
                                         theIscPair.getValue(),
                                         theProtocolPair.getName(),
                                         theProtocolPair.getValue());
    String request = "http://myipaddress:myport/myapplication/
    myapplication/sc/IDACall?isc_rpc=1&isc_v=v8.2p_2012-08-28&isc_xhr=1";
    URL url = new URL(request); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", 
   "application/x-www-form-urlencoded; charset=UTF-8");
    connection.setRequestProperty("CUSTOM-userid", "ghettosamson-testNG");
    connection.setUseCaches (false);
    DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();
    connection.disconnect();
 }`

0 个答案:

没有答案