gpt4 book ai didi

c# - HttpWebRequest:如何通过带有 x-www-form-enclosed 的 WebRequest 查找加拿大邮政的邮政编码?

转载 作者:行者123 更新时间:2023-11-30 17:24:08 27 4
gpt4 key购买 nike

我目前正在编写一些测试,以便提高我通过 Windows 窗体进行 Internet 交互的技能。其中一项测试是查找应由加拿大邮政网站返回的邮政编码。

  1. 我的默认 URL 设置为:http://www.canadapost.ca/cpotools/apps/fpc/personal/findByCity?execution=e4s1
  2. 必填的表单字段是:streetNumberstreetNamecityprovince
  3. 内容类型是“application/x-www-form-enclosed”

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

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

Shall I consider going the XML way? Is it first of all possible to search on Canada Post anonymously?

为了更好地描述,这里有一个代码示例:

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.
}

在我看来,我似乎陷入了瓶颈。我找不到想要的结果的出路。

EDIT: There seems to have to parameters as for the ContentType of this site. Let me explain.

  • 有一个带有“元”变量的变量,它规定了以下内容:

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

  • 还有一个后来的代码被读作:

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

我的问题如下:我必须坚持使用哪一个?

让我猜猜,第一个 ContentType 被认为是第二个仅用于另一个函数请求,或者在发布数据时?

EDIT: As per request, the closer to the solution I am is listed under this question: WebRequest: How to find a postal code using a WebRequest against this ContentType=”application/xhtml+xml, text/xml, text/html; charset=utf-8”?

感谢您的帮助! :-)

最佳答案

我想找出您不使用 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);

我在您的原始代码中发现的一些问题:-

  1. 第一个字段以 & 为前缀,不应该在那里。
  2. 您需要对每个字段值调用 Uri.EscapeDataString
  3. 您正在尝试围绕 GetRequestStream 的结果构建内存流,即使 MemoryStream 有这样的构造函数,我也看不到它会实现什么,但它没有无论如何。直接写入GetRequestStream
  4. 返回的流即可

如果您已经这样做了,请为自己准备一份 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;
}
}

现在使用我上面的代码来代替 od 实例化 WebClient 实例 MyWebClient

关于c# - HttpWebRequest:如何通过带有 x-www-form-enclosed 的 WebRequest 查找加拿大邮政的邮政编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1444563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com