我有一个通常指向邮政编码查询网络服务的网址:
"http://localhost/afddata.pce?Serial=xxxxxx&Password=<PASSWORD>&UserID=<UNAME>&Data=Address&Task=PropertyLookup&Fields=List&MaxQuantity=200&Lookup=BD1+3RA"
我需要调用这个url,可能是使用HttWebRequest并获取输出,这是一个xml字符串(参见示例):
<?xml version="1.0"?>
<AFDPostcodeEverywhere>
<Result>1</Result><ErrorText></ErrorText><Item value="1"><Postcode>BD1 3RA</Postcode>
<PostcodeFrom></PostcodeFrom>
<Key>BD1 3RA1001</Key>
<List>BD1 3RA City of Bradford Metropolitan District Council, Fountain Hall, Fountain Street, BRADFORD</List>
<CountryISO>GBR</CountryISO>
</Item>
</AFDPostcodeEverywhere>
我的问题是,当我在浏览器中输入网址时,我在浏览器中获取了上面的xml,但是我无法通过代码获取此xml字符串。任何人都可以帮我PLZ!它非常非常紧急!根据我的阅读,我们需要提出肥皂要求,但我不知道该怎么做!提前谢谢!
答案 0 :(得分:1)
就个人而言,我更喜欢使用XDocument.Load方法,然后使用LINQ-to-XML来读取结果。它比.NET框架中的其他解析机制更清晰。
var xmlDoc = XDocument.Load("http://localhost/afddata.pce?Serial=xxxxxx&Password=<PASSWORD>&UserID=<UNAME>&Data=Address&Task=PropertyLookup&Fields=List&MaxQuantity=200&Lookup=BD1+3RA");
然后,您可以使用LINQ来解析结果,或者调用ToString()以将XML作为字符串获取。
答案 1 :(得分:1)
您可以从HttpWebResponse对象获取XML响应(如果我记得的话,在System.Net命名空间下)。
要获得HttpWebResponse,首先必须构建一个HttpWebRequest对象。
见:
您可以使用以下代码将响应转换为可以遍历的XMLDocument:
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://pce.afd.co.uk/afddata.pce?...");
using (HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse())
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(HttpWResp.GetResponseStream());
}
有问题的公司似乎有一个ASMX webservice,您可以在.NET应用程序中使用它来获取必要的数据。
您需要密码和序列号(可以从URL获取)。
答案 2 :(得分:0)
答案很晚,但我正在使用RestSharp来实现这项确切的服务。
Robert W发现的Web服务无法满足我的需求,因为它需要2次调用才能获取完整的地址数据,一次获取匹配列表,一次获取所选地址。
XML服务提供的格式包含所有匹配结果的完整地址数据。
这是我的解决方案。
您需要自定义IAuthenticator
:
public class AfdAuthenticator : IAuthenticator
{
private readonly string serial;
private readonly string password;
private readonly string userId;
public AfdAuthenticator(string serial, string password, string userId)
{
this.serial = serial;
this.password = password;
this.userId = userId;
}
public void Authenticate(IRestClient client, IRestRequest request)
{
// AFD requires the authentication details to be included as query string parameters
request.AddQueryParameter("Serial", this.serial);
request.AddQueryParameter("Password", this.password);
request.AddQueryParameter("UserID", this.userId);
}
}
您需要回复课程:
[XmlRoot(ElementName = "AFDPostcodeEverywhere")]
public class AfdPostcodeEverywhere
{
[XmlElement(ElementName = "Result")]
public int Result { get; set; }
[XmlElement(ElementName = "ErrorText")]
public string ErrorText { get; set; }
[XmlElement(ElementName = "Items")]
public List<Item> Items { get; set; }
}
[XmlRoot(ElementName = "Item")]
public class Item
{
[XmlElement(ElementName = "AbbreviatedPostalCounty")]
public string AbbreviatedPostalCounty { get; set; }
[XmlElement(ElementName = "OptionalCounty")]
public string OptionalCounty { get; set; }
[XmlElement(ElementName = "AbbreviatedOptionalCounty")]
public string AbbreviatedOptionalCounty { get; set; }
[XmlElement(ElementName = "PostalCounty")]
public string PostalCounty { get; set; }
[XmlElement(ElementName = "TraditionalCounty")]
public string TraditionalCounty { get; set; }
[XmlElement(ElementName = "AdministrativeCounty")]
public string AdministrativeCounty { get; set; }
[XmlElement(ElementName = "Postcode")]
public string Postcode { get; set; }
[XmlElement(ElementName = "DPS")]
public string Dps { get; set; }
[XmlElement(ElementName = "PostcodeFrom")]
public string PostcodeFrom { get; set; }
[XmlElement(ElementName = "PostcodeType")]
public string PostcodeType { get; set; }
[XmlElement(ElementName = "Phone")]
public string Phone { get; set; }
[XmlElement(ElementName = "Key")]
public string Key { get; set; }
[XmlElement(ElementName = "List")]
public string List { get; set; }
[XmlElement(ElementName = "Locality")]
public string Locality { get; set; }
[XmlElement(ElementName = "Property")]
public string Property { get; set; }
[XmlElement(ElementName = "Street")]
public string Street { get; set; }
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Organisation")]
public string Organisation { get; set; }
[XmlElement(ElementName = "Town")]
public string Town { get; set; }
[XmlAttribute(AttributeName = "value")]
public int Value { get; set; }
}
我通过使用PostMan调用服务然后使用Xml2CSharp从XML生成类来生成这些
然后你可以使用这段代码:
// Create a RestClient passing in a custom authenticator and base url
var client = new RestClient
{
Authenticator = new AfdAuthenticator("YOUR SERIAL", "YOUR PASSWORD", "YOUR USERID"),
BaseUrl = new UriBuilder("http", "pce.afd.co.uk").Uri
};
// Create a RestRequest using the AFD service endpoint and setting the Method to GET
var request = new RestRequest("afddata.pce", Method.GET);
// Add the required AFD query string parameters
request.AddQueryParameter("Data", "Address");
request.AddQueryParameter("Task", "FastFind");
request.AddQueryParameter("Fields", "Simple");
request.AddQueryParameter("Lookup", "BD1 3RA");
// Execute the request expecting an AfdPostcodeEverywhere returned
var response = client.Execute<AfdPostcodeEverywhere>(request);
// Check that RestSharp got a response
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception(response.StatusDescription);
}
// Check that RestSharp was able to process the response
if (response.ResponseStatus != ResponseStatus.Completed)
{
throw new Exception(response.ErrorMessage, response.ErrorException);
}
var afdPostcodeEverywhere = response.Data;
// Check that AFD returned data
if (afdPostcodeEverywhere.Result < 0)
{
throw new Exception(afdPostcodeEverywhere.ErrorText);
}
// Process the results
var addresses = afdPostcodeEverywhere.Items;
数据,任务,字段和查找选项以及所有结果代码的完整详细信息可以在SDK中包含的AFD API文档中找到。