MonoTouch 自动代理网络凭据不起作用 [英] MonoTouch auto proxy network credentials not working

查看:29
本文介绍了MonoTouch 自动代理网络凭据不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图从我的服务器加载一些数据(xml 数据).我通常只使用 HttpWebRequest,因为我可以将 utomaticDecompression 设置为 DecompressionMethods.GZip |DecompressionMethods.Deflate 以便自动对我的数据使用 gzip 压缩.

Trying to load some data off my server (xml data). I normally just use HttpWebRequest as I can set the utomaticDecompression to DecompressionMethods.GZip | DecompressionMethods.Deflate in order to use gzip compression of my data automatically.

最近一位客户给我发电子邮件说它在他的学校内不起作用.通过 TestFlightApp 向他发送应用程序的另一个副本以从他那里获取更多调试信息,应用程序以 407 身份验证错误响应.从那以后,我在我的计算机上设置了一个代理,我将我的 iPhone 流量重定向到该代理以进行测试.

Recently a client has emailed me saying that it does not work from within his school. Sent out another copy of the app to him via TestFlightApp to get more debugging info from him and the app responds with a 407 auth error. Since then I have set up a proxy on my computer which I redirect my iPhone traffic through to test on.

我原来的设置是这样的,

My original setup was like so,

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.Proxy = null;
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
WebResponse response = request.GetResponse();

然后以此类推.

我将 Proxy 设置为 null,因为在大多数情况下,用户不会在代理后面,这会中断 1-2 秒的代理解析.稍后有代码可以捕获它,然后重新创建 HttpWebRequest 但不将代理清空,我认为这将是默认代理.显然不是这样...

I'd set Proxy to null because in most cases the user would not be behind a proxy and this cut off 1-2 seconds of proxy resolving. There is code later to catch this and then re-create the HttpWebRequest but by not nulling the proxy, which I thought would be default proxy. Apparently not so...

然后我尝试获取设备的默认代理设置.

I tried to then get the default proxy settings of the device.

CFProxySettings proxy0 = CFNetwork.GetSystemProxySettings();
IWebProxy proxy1 = WebRequest.GetSystemWebProxy();
IWebProxy proxy2 = HttpWebRequest.DefaultWebProxy;
IWebProxy proxy3 = HttpWebRequest.GetSystemWebProxy();
IWebProxy proxy4 = CFNetwork.GetDefaultProxy();

proxy1 - proxy4 的凭据为空.proxy0 包含我需要的代理信息,例如代理 url、端口、用户名,但没有密码...

proxy1 - proxy4 have null for the credentials. proxy0 contains proxy information which I need such as proxy url, port, username, but no password...

我用我的凭据手动创建了一个 WebProxy

I manually made a WebProxy with my credentials

WebProxy webProxy = new WebProxy(proxy0.HTTPProxy, proxy0.HTTPPort);
webProxy.Credentials = new NetworkCredential("user", "pass");
request.Proxy = webProxy;

这是有效的.未能在 407 错误中为代理请求用户名和密码,我不知道该怎么办...WebClient 下载不起作用,但 UIWebView 和 MKMapView 会自动进行身份验证.

and this works. Falling short of requesting username and password for the proxy on a 407 error I am not sure what to do... WebClient downloads do not work, but UIWebView and MKMapView does auto-auth itself.

对我缺少的东西有什么想法吗?

Any ideas what I am missing?

发现这个,https://bugzilla.xamarin.com/show_bug.cgi?id=1081

推荐答案

我今天看了这个,做了很多调试和浏览 Apple 的文档 - 恐怕我没有一个肯定的答案你 - 除非你认为不可能"是肯定的答案......

I had a look at this today, doing a lot of debugging and browsing Apple's Documentation - and I'm afraid, I don't have a positive answer for you - unless you consider "impossible" as a positive answer ...

一般问题

根据 Apple 的文档, CFProxySupport 不允许您访问存储的代理凭据.

According to Apple's Documentation, CFProxySupport does not give you access to the stored proxy credentials.

有一种 - 未公开 - 获取代理用户名的方法(返回的字典使用字符串作为键,其中一个键称为HTTPUser"或HTTPProxyUsername",但我找不到任何字符串常量),但不是密码.

There is an - undocumented - way of getting the proxy username (the returned dictionary uses strings as keys, one of these keys is called "HTTPUser" or "HTTPProxyUsername", but I couldn't find any string constant for it), but not the password.

在 Mac OS X 上

实际上在 Mac OS X 上有一个解决方案:当我运行一个简单的基于 NSURLConnection 的示例时,它要求我获得访问钥匙串的权限,所以我打开了钥匙串实用程序并找到了代理凭据在登录钥匙串中.我还写了一个简单的 Objective C 函数来检索它,但是您需要知道代理用户名(但我没有尝试使用 NULL,也许可以).

There is actually a solution on Mac OS X: when I ran a simple NSURLConnection based example, it asked me for permission to access the keychain, so I opened the Keychain Utility and found the proxy credentials in the login keychain. I also wrote a simple Objective C function to retrieve it, but you need to know the proxy user name (but I haven't tried using NULL, maybe that works).

我也找不到任何方法来检查是否需要身份验证,除了检查该用户名是否为 NULL.不幸的是,您需要钥匙串访问权限来检查某些项目是否存在,因此我们需要确保确实需要身份验证.一种方法是在 HttpWebConnection 已经收到 HTTP 407(其中还包含所需的用户名)后从内部进行.

I also couldn't find any way of checking whether authentication is required at all, except by checking whether that username is NULL. Unfortunately, you need keychain access privileges to check whether some item exists, so we need to make sure that authentication is actually requierd. One way of doing that would be from inside HttpWebConnection after it already received the HTTP 407 (and of these also contain the desired username).

在 iOS 上

从我目前在互联网上读到的内容来看,iOS 不允许您访问系统钥匙串 - 我什至不确定它是否存在.CFProxySupport 也不返回任何凭据.

From what I've read so far on the internet, iOS does not let you access the system keychain - I'm not even sure whether that exists at all. The CFProxySupport also doesn't return any credentials.

Fwiw.,除非您想在不使用 iOS API 的情况下进行 HTTP 访问,否则您也不需要知道代理密码.因此,很有可能无法取回它.

Fwiw., you also don't ever need to know the proxy password unless you want to do HTTP access without using the iOS APIs for it. So there's a decent chance that there's just no way of retrieving it.

作为一种解决方案,您可以使用 iOS API,例如 NSURLConnectionWebClientCFNetwork 处理程序.

As a solution, you could either use the iOS APIs such as NSURLConnection or WebClient with the CFNetwork handler.

这篇关于MonoTouch 自动代理网络凭据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆