WP8-登录网站并从答案解析HTML

本文关键字:HTML 答案 登录 网站 WP8- | 更新日期: 2023-09-27 17:58:56

我想登录此网站:http://subcard.subway.co.uk/de_cardholder/JSP/login_reg.jsp我发现我必须向服务器发送POST请求,我也知道我必须处理这个POST请求:

POST /de_cardholder/servlet/SPLoginServlet HTTP/1.1
Host: subcard.subway.co.uk
language=de&userID=ID&password=PASSWORD&transIdentType=1&programID=6

登录后,我想解析HTML数据。但是我如何在C#中实现WP上的POST请求,它像我想象的那么容易吗?

WP8-登录网站并从答案解析HTML

试试这个,

Uri RequestUri = new Uri("subcard.subway.co.uk/de_cardholder/servlet/SPLoginServlet HTTP/1.1?language=de&userID=ID&password=PASSWORD&transIdentType=1&programID=6", UriKind.Absolute);
string PostData = "";
WebRequest webRequest;
webRequest = WebRequest.Create(RequestUri);
webRequest.Method = "POST";
webRequest.ContentType = "text/xml";
HttpWebResponse response;
string Response;
using (response = (HttpWebResponse)await webRequest.GetResponseAsync()) ;
using (Stream streamResponse = response.GetResponseStream())
using (StreamReader streamReader = new StreamReader(streamResponse))
{
    Response = await streamReader.ReadToEndAsync();
}
if(Response != null)
{
    //data should be available in Response
}
var postRequest = (HttpWebRequest)WebRequest.Create("your Url here");
postRequest.ContentType = "application/x-www-form-urlencoded";// you can give the type of request content here
postRequest.Method = "POST";

如果您有任何数据要作为内容的一部分而不是URL本身的一部分与请求一起发布,您可以添加它。示例:-(language=de&userID=ID&password&transIdentType=1&programID=6)

using (var requestStream = await postRequest.GetRequestStreamAsync())
{
     byte[] postDataArray = Encoding.UTF8.GetBytes("your request data");
     await requestStream.WriteAsync(postDataArray, 0, postDataArray.Length);
}

如果您没有任何数据作为发送内容忽略上面的代码

WebResponse postResponse = await postRequest.GetResponseAsync();
if (postResponse != null)
{
    var postResponseStream = postResponse.GetResponseStream();
    var postStreamReader = new StreamReader(postResponseStream);
    string response = await postStreamReader.ReadToEndAsync();// Result comes here
}