我想知道在使用WebBrowser控件的重载“导航”方法后如何获取响应数据。以下是我正在做的一个例子:
wb.Navigate("https://live.xbox.com/en-US/Friends/List", "", Encoding.ASCII.GetBytes(post), "Content-Type: application/x-www-form-urlencoded\r\n");
这是将我的页面导航到第一个参数,但我不知道如何通过调用它来获取响应数据。我想要的结果也就是响应的主体。 有人可以帮助我吗我尝试使用WebRequest和WebResponse方式,但他们不允许我出于某种原因签署用户进入Xbox Live网站,导致我自己无法做任何事情并得到411错误。我希望能够使用WebBrowser执行此操作。但如果我不能,任何帮助将不胜感激。
答案 0 :(得分:0)
您可以使用webbrowser控件使用假网络点击事件等进行登录(您似乎已经这样做了,+1)
然后你可以将cookie从webbrowser控件转移到webrequest来做更多理智的东西。您也可以稍后缓存cookie
using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
namespace Sample.Helpers {
public class NativeMethods {
public static CookieContainer GetUriCookieContainer(Uri uri) {
CookieContainer cookies = null;
int datasize = 8192 * 16; // Determine the size of the cookie
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero)) {
if (datasize < 0)
return null;
cookieData = new StringBuilder(datasize); // Allocate stringbuilder large enough to hold the cookie
if (!InternetGetCookieEx(
uri.ToString(),
null, cookieData,
ref datasize,
InternetCookieHttponly,
IntPtr.Zero))
return null;
}
if (cookieData.Length > 0) {
cookies = new CookieContainer();
cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
}
return cookies;
}
[DllImport("wininet.dll", CharSet = CharSet.Unicode, SetLastError = true, ThrowOnUnmappableChar = true)]
private static extern bool InternetGetCookieEx(string url, string cookieName, StringBuilder cookieData, ref int size, Int32 dwFlags, IntPtr lpReserved);
private const Int32 InternetCookieHttponly = 0x2000;
}
}
使用示例
var webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.CookieContainer = Cookies;
//webRequest.Headers.Add(HeaderCollection);
webRequest.Timeout = 10000;
//webRequest.UserAgent = UserAgent;
webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
webRequest.Method = "POST";
byte[] postBytes;
if (postDataRaw != null) {
postBytes = Encoding.UTF8.GetBytes(postDataRaw);
}
webRequest.ContentLength = postBytes.Length;
using (var dataStream = webRequest.GetRequestStream()) {
dataStream.Write(postBytes, 0, postBytes.Length);
}
using (var response = webRequest.GetResponse()) {
var result = ((HttpWebResponse)response).StatusCode;
using (var responseStream = response.GetResponseStream()) {
using (var streamReader = new StreamReader(responseStream)) {
return streamReader.ReadToEnd();
}
}
}
答案 1 :(得分:-1)
尝试使用 System.Net.HttpWebRequest 而不是WebBrowser控件:
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://website.com/service");
request.Method = "GET";
StringBuilder data = new StringBuilder();
try
{
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
return reader.ReadToEnd();
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
String errorText = reader.ReadToEnd();
}
throw;
}
}
catch (Exception)
{
throw;
}
这应该是非常自我解释的,但总而言之,代码将使用HTTP GET 命令向远程Web服务器创建请求。然后可以从应用程序(控制台/ winform / service等等)解析和使用响应。
希望这有帮助。