gpt4 book ai didi

c# - 将 HttpClient 类与 WinRT 结合使用

转载 作者:太空狗 更新时间:2023-10-29 21:13:35 25 4
gpt4 key购买 nike

我有一段代码可以在 .NET (4.0) 中完美运行

代码#

        string URI = "http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi";
string Parameters = Uri.EscapeUriString("lccp_pnrno1=8561180607&submitpnr=Get Status");
System.Net.HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(URI);
//HTTP POST Headers
req.ContentType = "application/x-www-form-urlencoded";
req.Host = "www.indianrail.gov.in";
//You can use your own user-agent.
req.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0) DELL;Venue Pro";
req.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
req.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
req.KeepAlive = true;
req.Referer = "http://www.indianrail.gov.in/pnr_stat.html";
req.Accept = "text/plain";
req.Method = "POST";
//Byte size calculation before sending request.
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
System.Net.WebResponse resp = req.GetResponse();
var request_status = ((HttpWebResponse)resp).StatusDescription;
if (resp == null) return;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
Console.WriteLine(sr.ReadToEnd());
Console.ReadLine();

我似乎无法弄清楚在 Win Store 应用程序中写什么

到目前为止,我已经知道我必须使用(或可能不使用)哪个类了

HttpClient httpClient = new HttpClient();

httpClient.DefaultRequestHeaders.Add("Host", "www.indianrail.gov.in");

但是其他值呢?有一些 header ,我们可以在其中添加一些数据,还有一些需要直接添加到集合(DefaultHeaders)中...

是否有任何使用说明文档

在这方面的任何帮助都会很棒:)

最佳答案

任何方式感谢社区的帮助....一些阅读让我找到了这个解决方案

            string URI = "http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi";
string Parameters = Uri.EscapeUriString("lccp_pnrno1=8561180604&submitpnr=Get Status");

HttpClient client = new HttpClient();

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,URI);

request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8",0.7));
request.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-us",0.5));
request.Content = new StreamContent(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(Parameters)));
request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
request.Headers.Host = "www.indianrail.gov.in";
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("Mozilla", "5.0"));
request.Headers.Referrer = new Uri("http://www.indianrail.gov.in/pnr_stat.html");

var result = await client.SendAsync(request);
var content = await result.Content.ReadAsStringAsync();

这会返回我想要的结果

感谢新方法

关于c# - 将 HttpClient 类与 WinRT 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13903470/

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