HttpWebRequest: How to find the zip code in Canada Post via WebRequest with x-www-form-enclosed?

I am currently writing several tests to improve my Internet experience through Windows Forms. One of these tests is to find the postal code that should be returned by the Canada Post website.

EDIT: Please consider the value of "application / x-www-form-encoded" instead of the value of point 3 as contentType. (Thanks EricLaw-MSFT!)

The result that I get is not the expected result. I get the source code of the HTML page where I could manually enter the information to find the postal code, but not the HTML source with the postal code found. Any idea what I'm doing wrong?

Should I consider the XML path? Can I search anonymously in Canada first?

Here is a sample code for a better description:

public static string FindPostalCode(ICanadadianAddress address) {
   var postData = string.Concat(string.Format("&streetNumber={0}", address.StreetNumber)
    , string.Format("&streetName={0}", address.StreetName)
    , string.Format("&city={0}", address.City)
    , string.Format("&province={0}", address.Province));

   var encoding = new ASCIIEncoding();
   byte[] postDataBytes = encoding.GetBytes(postData);
   request = (HttpWebRequest)WebRequest.Create(DefaultUrlSettings);
   request.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Anonymous;
   request.Container = new CookieContainer();
   request.Timeout = 10000;
   request.ContentType = contentType;
   request.ContentLength = postDataBytes.LongLength;
   request.Method = @"post";
   var senderStream = new StreamWriter(request.GetRequestStream());
   senderStream.Write(postDataBytes, 0, postDataBytes.Length);
   senderStream.Close();
   string htmlResponse = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();

   return processedResult(htmlResponse); // Processing the HTML source code parsing, etc.
}

I seem to be stuck in the neck of the bottle from my point of view. I do not see a way out to the desired result.

EDIT: . It looks like the parameters for the ContentType of this site have parameters. Let me explain.

  • , "meta" -, :

meta http-equiv = "Content-Type" content = "application/xhtml + xml, text/xml, text/html; = UTF-8"

  • , :

form id = "fpcByAdvancedSearch: fpcSearch" name= "fpcByAdvancedSearch: fpcSearch" method = "post" action = "/cpotools/apps/fpc/personal/findByCity? execution = e1s1" enctype = "application/x-www --urlencoded"

: ?

, ContentType , , ?

: , , : WebRequest: WebRequest ContentType = "application/xhtml + xml, text/xml, text/html; charset = utf-8" ?

!: -)

+3
2

, WebClient: -

var fields = new NameValueCollection();
fields.Add("streetnumber", address.StreetNumber);
fields.Add("streetname", address.StreetName);
fields.Add("city", address.City);
fields.Add("province", address.Province);

var wc = new WebClient();
byte[] resultData = wc.UploadValues(url, fields);
string result = Encoding.Default.GetString(resultData);

, , UTF-8, : -

string result = Encoding.UTF8.GetString(resultData);

, : -

  • &, .
  • Uri.EscapeDataString .
  • GetRequestStream, , , MemoryStream , . , GetRequestStream

, fiddler, , , .

. , cookie , WebClient , : -

public class MyWebClient : WebClient
{

    protected override WebRequest GetWebRequest (Uri address)
    {
      WebRequest request = (WebRequest) base.GetWebRequest (address);

      request.Container = new CookieContainer();
      return request;
    }
}

, instancing WebClient instance MyWebClient.

+1

HTTPWebRequest URL-. HTML-, HTML-. XML, XML.

, -. , - -, . , XML, JSON . .

+1

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


All Articles