3

我想知道在使用 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 做到这一点。但如果我不能,任何帮助将不胜感激。

4

2 回答 2

0

您可以使用网络浏览器控件使用虚假的网络点击事件等登录(您似乎已经这样做了,+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();
        }
    }
}
于 2015-12-11T05:01:44.443 回答
-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/服务等)解析和使用响应

希望这可以帮助。

于 2013-08-18T03:21:11.483 回答