我不是在问这里的代码!我在这里询问流程。
我想通过XML gateway of Companies House搜索公司名称的可用性。我知道如何使用Java中的jabx编组和解组数据,但我不知道应用程序的确切流程。谁能告诉我从哪里开始?公司大楼提供sample XML request和sample XML response。
请找我的代码。
package Classes;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
public class PostXML {
public static void main(String args[]) throws FileNotFoundException {
// Get target URL
String strURL = "http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway" ;
// Get file to be posted
String strXMLFilename = "F:\\12-8\\CompanyFormation\\CompanyFormation\\web\\file.xml";
File input = new File(strXMLFilename);
// Prepare HTTP post
PostMethod post = new PostMethod(strURL);
// Request content will be retrieved directly
// from the input stream
// Per default, the request content needs to be buffered
// in order to determine its length.
// Request body buffering can be avoided when
// content length is explicitly specified
post.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(input), input.length()));
// Specify content type and encoding
// If content encoding is not explicitly specified
// ISO-8859-1 is assumed
post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
// Get HTTP client
HttpClient httpclient = new HttpClient();
// Execute request
try {
int result = httpclient.executeMethod(post);
// Display status code
System.out.println("Response status code: " + result);
// Display response
System.out.println("Response body: ");
System.out.println(post.getResponseBodyAsString());
} catch (Exception e) {
e.printStackTrace();
} finally {
// Release current connection to the connection pool
// once you are done
post.releaseConnection();
}
}
}
我生成的请求:
<GovTalkMessage xsi:schemaLocation="http://www.govtalk.gov.uk/CM/envelope http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd" xmlns="http://www.govtalk.gov.uk/CM/envelope" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<EnvelopeVersion>1.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>NameSearch</Class>
<Qualifier>request</Qualifier>
<TransactionID>1</TransactionID>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>XMLGatewayTestUserID</SenderID>
<Authentication>
<Method>CHMD5</Method>
<Value>XMLGatewayTestPassword</Value>
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys/>
</GovTalkDetails>
<Body>
<NameSearchRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema/NameSearch.xsd">
<CompanyName>SPECIALIST PENSION SERVICES LIMITED</CompanyName>
<DataSet>LIVE</DataSet>
<SameAs>0</SameAs>
<SearchRows>100</SearchRows>
</NameSearchRequest>
</Body>
</GovTalkMessage>
输出
响应状态代码:200 回应机构: Jan 07,2014 12:30:02 PM org.apache.commons.httpclient.HttpMethodBase getResponseBody 警告:要缓冲大型或未知大小的响应体。建议使用getResponseBodyAsStream。
<?xml version="1.0" encoding="UTF-8" ?>
<GovTalkMessage xsi:schemaLocation="http://www.govtalk.gov.uk/CM/envelope http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd" xmlns="http://www.govtalk.gov.uk/CM/envelope" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<EnvelopeVersion>1.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>NameSearch</Class>
<Qualifier>error</Qualifier>
<TransactionID>1</TransactionID>
<GatewayTimestamp>2014-01-07T06:59:50-00:00</GatewayTimestamp>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>XMLGatewayTestUserID</SenderID>
<Authentication>
<Method>CHMD5</Method>
<Value>XMLGatewayTestPassword</Value>
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys/>
<GovTalkErrors>
<Error>
<RaisedBy>NameSearch</RaisedBy>
<Number>502</Number>
<Type>fatal</Type>
<Text>Authorisation Failure</Text>
<Location></Location>
</Error>
</GovTalkErrors>
</GovTalkDetails>
<Body>
</Body>
</GovTalkMessage>
答案 0 :(得分:2)
<Class>
中的<MessageDetails>
标记无效。我相信你想要的价值是NameSearch
(没有空格)。