Note that there are some explanatory texts on larger screens.

plurals
  1. POWebRequest: How to find a postal code using a WebRequest against this ContentType="application/xhtml+xml, text/xml, text/html; charset=utf-8"?
    text
    copied!<p>I first posted this: <a href="https://stackoverflow.com/questions/1444563/httpwebrequest-how-to-find-a-postal-code-at-canada-post-through-a-webrequest-wit">HttpWebRequest: How to find a postal code at Canada Post through a WebRequest with x-www-form-enclosed?</a>.</p> <p>Following AnthonyWJones suggestions, I changed my code following his suggestions.</p> <p>On a continuation of my inquiry, I have noticed with time that the content-type of Canada Post is more likely to be <strong>"application/xhtml+xml, text/xml, text/html; charset=utf-8"</strong>.</p> <p>My questions are: </p> <ol> <li>How do we webrequest against such a content-type website?</li> <li>Do we have to keep on going with the NameValueCollection object?</li> <li>According to Scott Lance who generously provided me with precious information within my preceding question, the WebRequest shall return the type of information whatever the content-type may be, am I missing something here?</li> <li>Do I have to change my code because of the content-type change?</li> </ol> <p>Here is my code so that it might be easier to understand my progress.</p> <pre><code>internal class PostalServicesFactory { /// &lt;summary&gt; /// Initializes an instance of GI.BusinessSolutions.Services.PostalServices.Types.PostalServicesFactory class. /// &lt;/summary&gt; internal PostalServicesFactory() { } /// &lt;summary&gt; /// Finds a Canadian postal code for the provided Canadian address. /// &lt;/summary&gt; /// &lt;param name="address"&gt;The instance of GI.BusinessSolutions.Services.PostalServices.ICanadianCityAddress for which to find the postal code.&lt;/param&gt; /// &lt;returns&gt;The postal code found, otherwise null.&lt;/returns&gt; internal string FindPostalCode(ICanadianCityAddress address) { if (address == null) throw new InvalidOperationException("No valid address specified."); using (ServicesWebClient swc = new ServicesWebClient()) { var values = new System.Collections.Specialized.NameValueCollection(); values.Add("streetNumber", address.StreetNumber.ToString()); values.Add("numberSuffix", address.NumberSuffix); values.Add("suite", address.Suite); values.Add("streetName", address.StreetName); values.Add("streetDirection", address.StreetDirection); values.Add("city", address.City); values.Add("province", address.Province); byte[] resultData = swc.UploadValues(@"http://www.canadapost.ca/cpotools/apps/fpc/personal/findByCity", "POST", values); return Encoding.UTF8.GetString(resultData); } } private class ServicesWebClient : WebClient { public ServicesWebClient() : base() { } protected override WebRequest GetWebRequest(Uri address) { var request = (HttpWebRequest)base.GetWebRequest(address); request.CookieContainer = new CookieContainer(); return request; } } } </code></pre> <p>This code actually returns the HTML source code of the form one must fill with the required information in order to process with the postal code search. What I wish is to get the HTML source code or whatever it may be with the found postal code.</p> <blockquote> <p><strong>EDIT:</strong> Here's the WebException I get now: "Unable to send a content body with this type of verb." (This is a translation from the French exception "Impossible d'envoyer un corps de contenu avec ce type de verbe.")</p> </blockquote> <p>Here's my code:</p> <pre><code> internal string FindPostalCode(string url, ICanadianAddress address) { string htmlResult = null; using (var swc = new ServiceWebClient()) { var values = new System.Collections.Specialized.NameValueCollection(); values.Add("streetNumber", address.StreetNumber.ToString()); values.Add("numberSuffix", address.NumberSuffix); values.Add("suite", address.Suite); values.Add("streetName", address.StreetName); values.Add("streetDirection", address.StreetDirection); values.Add("city", address.City); values.Add("province", address.Province); swc.UploadValues(url, @"POST", values); string redirectUrl = swc.ResponseHeaders.GetValues(@"Location")[0]; =&gt; swc.UploadValues(redirectUrl, @"GET", values); } return htmlResult; } </code></pre> <p>The line that causes the exception is pointed with "=>". It seems that I can't use GET as the method, yet this is what has been told me me to do...</p> <p>Any idea what I'm missing here? I try to do what Justin (see answer) recommended me to do.</p> <p>Thanks in advance for any help! :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload