WebRequest.RegisterPrefix for http: // returns true, does not work

I am trying to use WebRequest.RegisterPrefix to register an IWebRequestCreate decorator with the intention of adding "debug" scripts (for example, emulating various connection scripts).

I use the Mango beta 2 SDK, and the RegisterPrefix method always returns true when used with "http: //" as the prefix (or "http" for that matter), but the registered instance of IWebRequestCreate is not used.

From the documentation, I can see that it should return false for duplicates, but it does not seem to function as documented.

Is there any other way to achieve what I need so that it is transparent to consumers?

+4
source share
3 answers

I use WebRequest.RegisterPrefix for unit testing, registering the implementation of IWebRequestCreate for the test:// prefix, and it works.

I found that after registering IWebRequestCreate for http:// calling WebRequest.Create using http:// uri will return a request created from a registered IWebRequestCreate , but calling WebRequest.CreateHttp will still return HttpWebRequest >.

The following code should verify this, and I am using the Mango Beta 2 SDK (6-29-11):

 public partial class MainPage : PhoneApplicationPage { public class FakeRequest : WebRequest { private Uri _uri; public FakeRequest(Uri uri) { _uri = uri; } public override Uri RequestUri { get { return _uri; } } } public class FakeRequestFactory : IWebRequestCreate { public WebRequest Create(Uri uri) { return new FakeRequest(uri); } } // Constructor public MainPage() { InitializeComponent(); // returns System.Net.Browser.ClientHttpWebRequest var request1 = WebRequest.Create("http://www.foo.com"); // returns System.Net.Browser.ClientHttpWebRequest var request2 = WebRequest.CreateHttp("http://www.foo.com"); // returns true bool result1 = WebRequest.RegisterPrefix("http://", new FakeRequestFactory()); // returns FakeRequest var request3 = WebRequest.Create("http://www.foo.com"); // returns System.Net.Browser.ClientHttpWebRequest var request4 = WebRequest.CreateHttp("http://www.foo.com"); // returns false bool result2 = WebRequest.RegisterPrefix("http://", new FakeRequestFactory()); // returns false, as per the note in the documention bool result3 = HttpWebRequest.RegisterPrefix("http://", new FakeRequestFactory()); } } 
+2
source

Hey, I know this question is 2 years old, but I ran into the same problem. I think you will find that WebRequest.RegisterPrefix() does returns false if you are trying to register http: (note a single colon, without a slash). If I ever find a workaround, I will try to remember this post.

EDIT

In my specific case, I wanted to throw away System.Net.FtpWebRequest and roll back my own implementation of the FTP client (because the framework implementation sucks ).

To do this, I used reflection (and a bunch of late linking tricks) to get the arraylist of the registered prefix and remove those that are associated with the System.Net.FtpWebRequestCreator inner class.

I'm not sure if all these APIs are available for a Windows phone, but here is what I did:

 Type webRequest = typeof(System.Net.WebRequest); Assembly system = Assembly.GetAssembly(webRequest); Type ftpWebRequestCreator = system.GetType("System.Net.FtpWebRequestCreator"); ArrayList prefixList = (ArrayList)webRequest.GetProperty("PrefixList", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null, null); IEnumerator enumerator = prefixList.GetEnumerator(); while (enumerator != null && enumerator.MoveNext()) { if (object.ReferenceEquals(enumerator.Current.Creator.GetType(), ftpWebRequestCreator)) { prefixList.Remove(enumerator.Current); if (System.Net.WebRequest.RegisterPrefix(enumerator.Current.Prefix, new CustomWebRequestCreator())) { enumerator = null; } else { enumerator = prefixList.GetEnumerator(); } } } // Now I can use Create() on the base class System.Net.WebRequest myCustomWebRequest = System.Net.WebRequest.Create("ftp://example.com/public"); 

This allows you to find all the prefixes registered in the FtpWebRequestCreator and replacing them with your own creator. It should be pretty simple to adapt this for http (s).

0
source

The phone has only a client stack, so RegisterPrefix does not affect the phone.

-1
source

All Articles