none
HttpWebRequest in c# do not work with .net 4.5 RRS feed

  • Question

  • I'm working on an c# project that send a xml to a server and receives a xml as response.
    With .Net Framework 4.0 installed that works fine.
    With .Net Framework 4.5 installed it throws this Exception:

    System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.  
    bei System.DomainNameHelper.IdnEquivalent(String hostname)  
    bei System.Uri.get_IdnHost()  
    bei System.Net.HttpWebRequest.GetSafeHostAndPort(Uri sourceUri, Boolean addDefaultPort, Boolean forcePunycode)  
    bei System.Net.HttpWebRequest.GenerateProxyRequestLine(Int32 headersSize)  
    bei System.Net.HttpWebRequest.SerializeHeaders()  
    bei System.Net.HttpWebRequest.EndSubmitRequest()  
    bei System.Net.HttpWebRequest.CheckDeferredCallDone(ConnectStream stream)  
    bei System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)  
    bei Fahrzeugverwaltungsserver.OutsideWorld.EXCAMPLE.RawCommunication.ISServer.doPostAndGet()

    I use the method BeginGetResponse and all parameters there are not null.
    Does anybody know what's wrong?
    Why does it work with 4.0 but not with 4.5?
    Did i forget something to set up?

    private void doPostAndGet() { try { //caching inform(SystemIcons.Information, Translations.ISServer_postAndGet_0); Trace.TraceInformation("OUT:\n" + Beautify(InputXML)); string c = cache.Get(InputXML.OuterXml); if (c != null) { XmlDocument docl = new XmlDocument(); docl.LoadXml(c); inform(SystemIcons.Information, Translations.ISServer_postAndGet_1); printInDocument(docl, "Aus Cache."); this.doc = docl; } //Read access information: UriBuilder urib = new UriBuilder("http", MANHaendlerdaten.IS_host, 9005, MANHaendlerdaten.IS_path); urib.UserName = MANHaendlerdaten.IS_user; urib.Password = MANHaendlerdaten.IS_password; String proxyUser = MANHaendlerdaten.IS_proxy_user; String proxyPassword = MANHaendlerdaten.IS_proxy_password; // create credentials for request's header: var proxy = Convert.ToBase64String( Encoding.UTF8.GetBytes(proxyUser + ":" + proxyPassword));
    var user = Convert.ToBase64String( Encoding.UTF8.GetBytes(urib.UserName + ":" + urib.Password)); //set proxy when needed: try { WebRequest.DefaultWebProxy = new WebProxy(MANHaendlerdaten.IS_proxy_ip, MANHaendlerdaten.IS_proxy_port); if (WebRequest.DefaultWebProxy == null) Trace.WriteLine(String.Format("WebRequest.DefaultWebProxy ist null. {0}, {1}", MANHaendlerdaten.IS_proxy_ip, MANHaendlerdaten.IS_proxy_port)); } catch (Exception e) { Trace.TraceError("1\n" + e.ToString()); Debug.WriteLine(Translations.ISServer_postAndGet_3); WebRequest.DefaultWebProxy = null; //speed up further request by avoiding proxy-auto-detect //pass when no proxy specified } // System.Net.ServicePointManager.Expect100Continue = false //this is a nasty one if not set to false client = (HttpWebRequest)WebRequest.Create(urib.Uri); //Encodings: client.Headers.Add("Accept-Encoding", "deflate"); client.ContentType = "text/xml; charset=UTF-8"; client.Accept = "text/xml; charset=UTF-8"; client.Headers.Add("SOAPAction", "\"\""); //Authentification: client.Headers.Add("Proxy-Authorization", "Basic " + proxy); client.Headers.Add("Authorization", "Basic " + user); //Connection and Protocol: client.Host = urib.Host; client.UserAgent = Translations.FullServiceName; client.ProtocolVersion = HttpVersion.Version10; client.KeepAlive = true; client.Method = WebRequestMethods.Http.Post; client.Timeout = 60000; client.Proxy = new WebProxy(MANHaendlerdaten.IS_proxy_ip, MANHaendlerdaten.IS_proxy_port); if (client.Proxy == null) Trace.WriteLine(String.Format("client.Proxy ist null. {0}, {1}", MANHaendlerdaten.IS_proxy_ip, MANHaendlerdaten.IS_proxy_port)); client.ReadWriteTimeout = 60000; //accept cookies within this ISServer-instance if (this.cookieCont == null) { this.cookieCont = new CookieContainer(); } client.CookieContainer = cookieCont; inform(SystemIcons.Information, Translations.ISServer_postAndGet_7); //Post request: using (Stream to_request = client.GetRequestStream()) { InputXML.Save(to_request); to_request.Flush(); } RequestState myRequestState = new RequestState(); myRequestState.request = client; webrequestresponse = false; IAsyncResult asyncResult = client.BeginGetResponse(new AsyncCallback(FinishWebRequest), myRequestState); while (webrequestresponse == false) { Thread.Sleep(100); } } catch (Exception e) { Trace.TraceError(e.ToString()); throw e; } }

    Wednesday, August 14, 2013 6:36 AM

Answers

All replies

  • Hi,

    From your information, I know it works very well in .net 4, but not work in .net 4.5.

    Can you please email on netfx45compat at Microsoft dot com(netfx45compat@microsoft.com) with following information:

    1. Code that reproduces the problem.

    2. Operating System

    And maybe you can use the httpclient to have a try.

    For more information, please try to refer to:
    #HttpClient & HttpWebRequest Confusion with HTTP POST:
    http://sylvester-lee.blogspot.in/2013/04/httpclient-httpwebrequest-confusion.html .

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Thursday, August 15, 2013 9:39 AM
    Moderator
  • I ran into this problem while working with the latest version of Amazon Web Services SDK for .NET 4.5, and it is caused by using http proxy. Either by using Fiddler, which turns on http proxy in the WinInet settings, or by placing http proxy in the .config causes the same exception. System acts as if http proxy is not there when it fact it is.

    To reproduce (requires AWS account):

    1. Get and build AWS SDK for .NET from https://github.com/aws/aws-sdk-net.git.
    2. Get modified ASW SDK sample from https://dl.dropboxusercontent.com/u/18391920/AWS_ConsoleSample.zip
      It is a modified version of the sample from http://sdk-for-net.amazonwebservices.com/latest/AWSSDKAndSamples.zip.
    3. Before building modified AWS_ConsoleSample, update the AWSSDK assembly reference to point to the AWS SDK for .NET 4.5 built at step 1.
    4. Modify AWS_ConsoleSample app.config to use your own AWSAccessKey and AWSSecretKey.
    5. Run the app, observe all is good.
    6. Start Fiddler, run the app - see the exception.
    7. Quit Fiddler, uncomment http proxy setting in app's app.config, run the app, see the same exception.



    • Edited by Vlad Hrybok Wednesday, January 15, 2014 4:47 PM
    Wednesday, January 15, 2014 4:22 PM