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

时间:2014-04-17 11:55:08

标签: c# html post windows-phone-8 login

我想登录此网站: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请求并且它是否像我想的那样容易?

2 个答案:

答案 0 :(得分:0)

试试这个,

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
}

答案 1 :(得分:0)

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