Windows phone 7 WP7和WP8中httpwebresponse的WebBrowser控件中的加载问题
我试图通过将httpwebresponse中的html代码存储在字符串变量中来加载web浏览器 我通过在其中设置cookies并在httpwebResponse中获取html页面,为URL创建了httpwebrequest 我试图通过将代码保存在字符串变量中,将其显示在web浏览器控件中。以及浏览browserbrowse.NavigateToStringstring 但这里的页面没有正确加载,它没有显示任何css或图像,也没有显示一条消息,您需要打开java脚本才能显示内容 在导航之前,我将web浏览器属性IsScriptEnabled设置为true。但它仍然显示消息和内容未正确加载 Cookie也不会被注入到web浏览器控件中。应用程序不会在整个应用程序中维护会话 是否有任何解决方案或解决方法 请参阅以下代码: 要从登录服务记录Cookie,请执行以下操作: 私人无效后登录请求 {Windows phone 7 WP7和WP8中httpwebresponse的WebBrowser控件中的加载问题,windows-phone-7,windows-phone-8,httpwebrequest,webbrowser-control,httpwebresponse,Windows Phone 7,Windows Phone 8,Httpwebrequest,Webbrowser Control,Httpwebresponse,我试图通过将httpwebresponse中的html代码存储在字符串变量中来加载web浏览器 我通过在其中设置cookies并在httpwebResponse中获取html页面,为URL创建了httpwebrequest 我试图通过将代码保存在字符串变量中,将其显示在web浏览器控件中。以及浏览browserbrowse.NavigateToStringstring 但这里的页面没有正确加载,它没有显示任何css或图像,也没有显示一条消息,您需要打开java脚本才能显示内容 在导航之前,我将w
string AuthServiceUri = "Service URL";
HttpWebRequest AuthReq = HttpWebRequest.Create(AuthServiceUri) as HttpWebRequest;
AuthReq.CookieContainer = new CookieContainer();
AuthReq.ContentType = "application/x-www-form-urlencoded";
AuthReq.Method = "POST";
AuthReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), AuthReq);
}
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest Request = (HttpWebRequest)callbackResult.AsyncState;
Stream postStream = Request.EndGetRequestStream(callbackResult);
string postData = "Postdata parameters";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), Request);
}
void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
try
{
HttpWebRequest callBackRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
CookieCollection cookiecollec = new CookieCollection();
cookiecollec = response.Cookies;
App.cookieCollection = cookiecollec; //global varaiable
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Uri ServiceUri = new Uri(PostURL); //Service URL
HttpWebRequest requestURL = HttpWebRequest.CreateServiceUri as HttpWebRequest;
requestURL.ContentType = "application/x-www-form-urlencoded";
requestURL.Method = "POST";
requestURL.CookieContainer = new CookieContainer();
requestURL.CookieContainer.Add(ServiceUri, App.cookieCollection);
requestURL.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), requestURL);
}
try
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
// MessageBox.Show(response.Cookies.ToString());
string responseString = "<html><head></head><body>";
Stream streamResponse = response.GetResponseStream();
StreamReader reader = new StreamReader(streamResponse);
responseString = responseString + reader.ReadToEnd() + "</body></html>";
streamResponse.Close();
reader.Close();
response.Close();
this.Dispatcher.BeginInvoke(() =>
{
webBrowser.IsScriptEnabled = true;
webBrowser.NavigateToString(responseString);
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
另一个带有已记录Cookie的httpwebrequest:
私有void ResponsivePostURLstring页面名、字符串PostURL、字符串PostDatas
{
string AuthServiceUri = "Service URL";
HttpWebRequest AuthReq = HttpWebRequest.Create(AuthServiceUri) as HttpWebRequest;
AuthReq.CookieContainer = new CookieContainer();
AuthReq.ContentType = "application/x-www-form-urlencoded";
AuthReq.Method = "POST";
AuthReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), AuthReq);
}
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest Request = (HttpWebRequest)callbackResult.AsyncState;
Stream postStream = Request.EndGetRequestStream(callbackResult);
string postData = "Postdata parameters";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), Request);
}
void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
try
{
HttpWebRequest callBackRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
CookieCollection cookiecollec = new CookieCollection();
cookiecollec = response.Cookies;
App.cookieCollection = cookiecollec; //global varaiable
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Uri ServiceUri = new Uri(PostURL); //Service URL
HttpWebRequest requestURL = HttpWebRequest.CreateServiceUri as HttpWebRequest;
requestURL.ContentType = "application/x-www-form-urlencoded";
requestURL.Method = "POST";
requestURL.CookieContainer = new CookieContainer();
requestURL.CookieContainer.Add(ServiceUri, App.cookieCollection);
requestURL.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), requestURL);
}
try
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
// MessageBox.Show(response.Cookies.ToString());
string responseString = "<html><head></head><body>";
Stream streamResponse = response.GetResponseStream();
StreamReader reader = new StreamReader(streamResponse);
responseString = responseString + reader.ReadToEnd() + "</body></html>";
streamResponse.Close();
reader.Close();
response.Close();
this.Dispatcher.BeginInvoke(() =>
{
webBrowser.IsScriptEnabled = true;
webBrowser.NavigateToString(responseString);
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void GetResponseStreamCallbackIAsyncResult callbackResult
{
string AuthServiceUri = "Service URL";
HttpWebRequest AuthReq = HttpWebRequest.Create(AuthServiceUri) as HttpWebRequest;
AuthReq.CookieContainer = new CookieContainer();
AuthReq.ContentType = "application/x-www-form-urlencoded";
AuthReq.Method = "POST";
AuthReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), AuthReq);
}
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest Request = (HttpWebRequest)callbackResult.AsyncState;
Stream postStream = Request.EndGetRequestStream(callbackResult);
string postData = "Postdata parameters";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), Request);
}
void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
try
{
HttpWebRequest callBackRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
CookieCollection cookiecollec = new CookieCollection();
cookiecollec = response.Cookies;
App.cookieCollection = cookiecollec; //global varaiable
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Uri ServiceUri = new Uri(PostURL); //Service URL
HttpWebRequest requestURL = HttpWebRequest.CreateServiceUri as HttpWebRequest;
requestURL.ContentType = "application/x-www-form-urlencoded";
requestURL.Method = "POST";
requestURL.CookieContainer = new CookieContainer();
requestURL.CookieContainer.Add(ServiceUri, App.cookieCollection);
requestURL.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), requestURL);
}
try
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
// MessageBox.Show(response.Cookies.ToString());
string responseString = "<html><head></head><body>";
Stream streamResponse = response.GetResponseStream();
StreamReader reader = new StreamReader(streamResponse);
responseString = responseString + reader.ReadToEnd() + "</body></html>";
streamResponse.Close();
reader.Close();
response.Close();
this.Dispatcher.BeginInvoke(() =>
{
webBrowser.IsScriptEnabled = true;
webBrowser.NavigateToString(responseString);
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
当您导航到站点并保存文档时,您不会得到脚本、css或图像文件以及任何其他资源,而只会得到静态/生成的HTML文档,它们可能使用相对路径引用这些文件。如果路径不是绝对路径,则找不到脚本/css/图像/其他资源 如果要使其正常工作,需要将HTML文件中的相对路径替换为绝对路径 所以如果你有 你会用
请注意,这只是我的最佳猜测,而不是我实际尝试过的:您确定从WebResponse接收到的html内容是正确的吗?只是好奇…尝试将响应保存在记事本文件中,将扩展名设置为.html并尝试在IE desktop中打开。这很奇怪。我正在使用WP7.x/WP8.x从例如下载动态内容.EA网站大量使用javascript和JSON内容,一切正常。也许你可以在这里复制/粘贴一些核心代码?@Abhishek..如何测试web响应中的html内容是否正确???@Mikki Viitala请参考下面的代码示例。我面临的主要问题是我需要在hybri中维护一个会话d应用程序。一旦用户登录应用程序。我正在尝试保存cookies并将cookies注入web浏览器以供进一步请求,以便用户登录到会话结束。我保存了html代码并在IE桌面中打开。它显示的页面与windows phone web浏览器中的页面相同,没有图像css。