Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用数据向servlet发送POST_Java_Servlets_Testng_Smartgwt_Smartclient - Fatal编程技术网

Java 使用数据向servlet发送POST

Java 使用数据向servlet发送POST,java,servlets,testng,smartgwt,smartclient,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 _t

我已经成功地使用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>`
收到的响应将写入文件,如下所示:

`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>`
`




















//isc_RPCResponseStart-->[{状态:0,数据:http://myipaddress:myport/myapplication /reports/ghettosamson-cURL1.pdf“}]//isc\u rpresponseend `
我试图用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();
 }`
`@Autowired@Qualifier(“contentHeader”)
专用标题-第二标题;
@自动连线@Qualifier(“userHeader”)
私有头用户头;
@自动连线@限定符(“transactionPair”)
私有名称值对交易对;
@自动连线@限定符(“iscPair”)
私有名称值对其配对;
@自动连线@限定符(“协议对”)
私有名称值与ProtocolPair配对;
@试验
public void testConcurrentCreateAndOpenReportRequests()
抛出ClientProtocolException,IOException
{
Assert.notNull(testpost);
Assert.notNull(contentheader);
Assert.notNull(userheader);
Assert.notNull(事务对);
HttpClient HttpClient=新的DefaultHttpClient();
testpost.addHeader(contentheader);
testpost.addHeader(userheader);
ArrayList参数=新的ArrayList();
参数。添加(事务对);
添加(新的BasicNameValuePair(“isc_tnum”,“10”);
添加(新的BasicNameValuePair(“protocolVersion”,“1.0”);
setEntity(新的UrlEncodedFormEntity(参数,“UTF-8”);
HttpResponse response=httpclient.execute(testpost);
HttpEntity=response.getEntity();
Assert.notNull(实体);
InputStream instream=entity.getContent();
System.out.println(instream.toString());
流内关闭();
}
@试验
public void testSimplePost()引发HttpException,IOException
{
HttpClient=新的HttpClient();
PostMethod方法=新的PostMethod(“http://myipaddress:myport/
myapplication/myapplication/sc/IDACall?isc_rpc=1&isc_v=v8.2p_2012-08-28&
isc_xhr=1“;
方法addRequestHeader(contentHeader);
addRequestHeader方法(userHeader);
方法.添加参数(事务对);
方法.添加参数(“isc_tnum”,“10”);
方法.addParameter(“protocolVersion”,“1.0”);
int statuscode=client.executeMethod(方法);
如果(状态代码!=-1)
{
InputStream instream=method.getResponseBodyAsStream();
系统输出打印LN(流内);
}
}   
@试验
public void testanmalypost()引发IOException
{
字符串urlParameters=String.format(“%s=%s&%s=%s&%s=%s”,
TransactionPair.getName(),
TransactionPair.getValue(),
theIscPair.getName(),
theIscPair.getValue(),
theProtocolPair.getName(),
protocolPair.getValue());
字符串请求=”http://myipaddress:myport/myapplication/
myapplication/sc/IDACall?isc_rpc=1&isc_v=v8.2p_2012-08-28&isc_xhr=1”;
URL=新URL(请求);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoOutput(真);
connection.setDoInput(true);
connection.setInstanceFollowDirections(false);
connection.setRequestMethod(“POST”);
connection.setRequestProperty(“内容类型”,
“application/x-www-form-urlencoded;charset=UTF-8”);
setRequestProperty(“自定义用户ID”、“ghettosamson testNG”);
connection.setUseCaches(false);
DataOutputStream wr=新的DataOutputStream(connection.getOutputStream());
writeBytes(URL参数);
wr.flush();
wr.close();
连接断开();
}`

如果没有异常的stacktrace(您可以使用Pastebin或Gist将其共享给我们),则很难诊断此异常。您可以在此处看到第一个方法的堆栈跟踪: