我有一个url,它通常指向邮政编码查找url服务:
"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>
我的问题是,当我在浏览器中输入URL时,我在浏览器中获得了上面的XML,但我无法通过代码获得这个XML字符串。据我所知,我们需要发出一个soap请求,但我不知道如何做到这一点。
发布于 2009-12-02 03:46:34
您可以从HttpWebResponse对象(如果我没记错的话,在System.Net名称空间下)获取XML响应。
要获取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());
}
编辑(1):
有问题的公司似乎有一个ASMX webservice,您可以在.NET应用程序中使用它来获取必要的数据。
您需要密码和序列号(可以从URL获得)。
发布于 2009-12-02 03:33:49
就我个人而言,我更喜欢使用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。
发布于 2015-10-29 23:06:55
我很晚才回答这个问题,但是我正在使用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文档。
https://stackoverflow.com/questions/1828289
复制相似问题