2013-11-26 46 views
1

我不問這裏的代碼!我在詢問這裏的流程。使用Companies House XML網關搜索公司名稱可用性

我想通過XML gateway of Companies House搜索公司名稱可用性。我知道如何使用Java中的jabx編組和解組數據,但我不知道應用程序的確切流程。任何人都可以告訴我從哪裏開始? Companies House提供sample XML requestsample 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 響應主體: 2014年1月7日下午12點30分02秒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

http://stackoverflow.com/questions/15085648/jaxb-unmarshalled-fields-are-null –

+0

你是什麼意思的「*流*」?也許你可以發佈你已經寫好的代碼,並解釋你卡在哪裏? –

+0

hello @Duncan我已經發布了我的java代碼以及傳遞的xml和輸出xml,請提出建議。 –

回答

2

<Class>標籤在<MessageDetails>是無效的。我相信你想要的價值是NameSearch(沒有空間)。

+0

你好湯姆!謝謝,這解決了我的608錯誤,但我現在得到502認證錯誤,雖然我已經輸入了正確的測試憑據,請看看我編輯的問題和代碼。 –