简体   繁体   English

如何使用PCL的HttpWebRequest禁用AllowAutoRedirect

[英]How to disable AllowAutoRedirect with PCL's HttpWebRequest

I'm rewriting my WP7's lib to PCL. 我正在将WP7的lib重写为PCL。 I have a function which uses HttpWebRequest's AllowAutoRedirect = false; 我有一个使用HttpWebRequest的AllowAutoRedirect = false的函数 property. 属性。 It's very important for correct log-in. 这对于正确登录非常重要。 I was disappointed when tried just copy-paste this function to PCL project. 尝试将此功能复制粘贴到PCL项目中时,我感到很失望。

Error   1   'System.Net.HttpWebRequest' does not contain a definition for 'AllowAutoRedirect' and no extension method 'AllowAutoRedirect' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?)

Is any workaround how to fix it? 有什么解决方法如何解决?

You cannot do this in your PCL project if it targets Windows Phone 7. You can however create a Windows Phone 7 Library with Visual Studio Express 2010 for Windows Phone: 如果PCL项目的目标是Windows Phone 7,则无法在PCL项目中执行此操作。但是,可以使用Visual Studio Express 2010 for Windows Phone创建Windows Phone 7库:

http://www.visualstudio.com/en-US/products/visual-studio-express-vs http://www.visualstudio.com/zh-CN/products/visual-studio-express-vs

You can still keep your code in the PCL and just inject the HttpWebrequest from the Windows Phone 7 library project. 您仍然可以将代码保留在PCL中,只需从Windows Phone 7库项目中注入HttpWebrequest。 I know it's messy but it may be the only way to accomplish it without having to have duplicate libraries for different platforms. 我知道它很杂乱,但是它可能是实现它的唯一方法,而不必为不同平台使用重复的库。

I've found solution on msdn forums but need to install portable http cline library from nuget first: 我在msdn论坛上找到了解决方案,但需要先从nuget安装可移植的http cline库:

Install-Package Microsoft.Net.Http

Here is peace of my modified code. 这是我修改后的代码的安全性。

HttpClientHandler hch = new HttpClientHandler();
hch.AllowAutoRedirect = false;
HttpClient hc = new HttpClient(hch);

StringContent queryString = new StringContent(string.Format("login={0}&password={1}", Uri.EscapeUriString(username), Uri.EscapeUriString(password));
queryString.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");

HttpResponseMessage msg = await hc.PostAsync("http://www....", queryString);
string responseBody = await msg.Content.ReadAsStringAsync();
...

Hope it will help to somebody 希望对别人有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用HttpWebRequest.AllowAutoRedirect处理身份验证? - How to handle authenticatication with HttpWebRequest.AllowAutoRedirect? 如何在Redrected页面上进行GET / POST时,让HttpWebRequest.AllowAutoRedirect设置cookie? - How to get HttpWebRequest.AllowAutoRedirect to set the cookies when doing a GET/POST on the redrected page? 内容配置的HttpWebRequest.AllowAutoRedirect问题 - HttpWebRequest.AllowAutoRedirect problem with Content-Disposition HttpWebRequest.AllowAutoRedirect = false会导致超时吗? - HttpWebRequest.AllowAutoRedirect=false can cause timeout? 如何在WinRT的HttpWebRequest中禁用“Expect:100 continue”标头 - How to disable the “Expect: 100 continue” header in WinRT's HttpWebRequest WPF上的PCL HttpWebRequest User-Agent - PCL HttpWebRequest User-Agent on WPF 禁用HttpWebRequest的图像下载 - Disable image download for HttpWebRequest 如何为Windows Phone应用程序禁用异步HTTPWebRequest的缓存? - How to disable cache for asynchronous HTTPWebRequest for Windows Phone application? "如何为单个请求禁用 HttpWebRequest 中的“期望:100 继续”标头?" - How to disable the "Expect: 100 continue" header in HttpWebRequest for a single request? WindowsPhone中的AllowAutoRedirect - AllowAutoRedirect in windowsPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM