WebRequest: how to find a zip code using WebRequest against this ContentType = "application / xhtml + xml, text / xml, text / html; charset = utf-8"?

I first posted this: HttpWebRequest: how to find the zip code in Canada Post via WebRequest with x-www-form app? .

Following the suggestions of AnthonyWJones, I changed my code after its suggestions.

In continuation of my request, I noticed over time that the Canada Post content type is more likely to be "application / xhtml + xml, text / xml, text / html; charset = utf-8" .

My questions:

  • How do we run a website with such a content site?
  • Do we need to continue working with the NameValueCollection object?
  • According to Scott Lance, who generously provided me with valuable information in my previous question, WebRequest will return the type of information regardless of the type of content, am I missing something here?
  • Do I need to change my code due to a change in the type of content?

Here is my code to make it easier to understand my progress.

internal class PostalServicesFactory {
/// <summary>
/// Initializes an instance of GI.BusinessSolutions.Services.PostalServices.Types.PostalServicesFactory class.
/// </summary>
internal PostalServicesFactory() {
}
/// <summary>
/// Finds a Canadian postal code for the provided Canadian address.
/// </summary>
/// <param name="address">The instance of GI.BusinessSolutions.Services.PostalServices.ICanadianCityAddress for which to find the postal code.</param>
/// <returns>The postal code found, otherwise null.</returns>
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;
    }
}
}

This code actually returns the source code of the HTML form, which must be filled with the necessary information to process the search in the zip code. I want to get the HTML source code or something else that might be with the found zip code.

EDIT: Here I get a WebException: "Failed to send the body of the content using this type of verb." (This is a translation from the French exception, "Impossible d'envoyer un corps de contenu avec ce type de verbe.")

Here is my 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];
        => swc.UploadValues(redirectUrl, @"GET", values);
    }

    return htmlResult;
}

, , "= > ". , GET , , , ...

, ? , (. ).

!: -)

+3
1

, ! :

  • - ,
  • URL-.
  • URL-, , ( HTTP 302) URL-, HTML, .

, №3 cookie, # 1. CookieContainer ( CookieContainer # 2 # 3).

, HTTP- , Accept. , , HttpWebRequest - , HTTP, .

, HttpWebRequest AllowAutoRedirect false . , , URL- HttpWebResponse Location:. HttpWebRequest ( GET-, POST) URL-. , cookie! (CookieContainer )

(№1 ), cookie . , , , , , .

Fiddler (www.fiddlertool.com), . Fiddler HTTP-, , ( ) HTTP-, , .

+2

Source: https://habr.com/ru/post/1718031/


All Articles