6

我正在开发一个将 xml 发送到服务器并接收 xml 作为响应的 c# 项目。
安装 .Net Framework 4.0 后运行良好。
安装 .Net Framework 4.5 后会抛出此异常:

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.MAN_Integrationsserver.RawCommunication.ISServer.doPostAndGet()`  

我使用的方法BeginGetResponse和所有参数都不为空。
有人知道出了什么问题吗?
为什么它适用于 4.0 但不适用于 4.5?
我忘记设置什么了吗?

编辑 1

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;
        }
}  

编辑 2
在我的配置文件中,我主要使用 appsettings 进行个人设置。喜欢:
<add key="DATABASE_CONNECTION" value="FIREBIRD"/>

4

4 回答 4

9

老实说,您现在的目标是 .NET 4.5,我会考虑使用HttpClient而不是 HttpWebRequest。

HttpClient

于 2013-08-13T08:23:36.093 回答
6

奇怪的是已经发布了一个没有认真的答案......

HttpWebRequest 在 .NET 4.5 及更高版本中已过时,因此它不会编译,这在msdn 网站上有所说明

这就是为什么 Jammer 说要使用HttpClient作为替代品,因为它似乎是微软为它做的替代品。

看起来这将是改变你的代码的努力,但我会说这一切都是最好的

于 2013-12-19T07:00:21.407 回答
1

我在 IdnEquivalent 中遇到了同样的奇怪问题。当我在调试会话期间启动 Fiddler Web 调试器时,问题仍然存在。关闭后问题就消失了。

于 2013-10-02T04:31:23.213 回答
0

如前所述,HttpWebRequest 现在已经过时了。你试过HttpRequest吗?在这里找到:https ://msdn.microsoft.com/en-us/library/system.web.httprequest(v=vs.110).aspx

于 2016-02-24T00:39:31.357 回答