Webservice報錯用戶端發現響應內容類型為“application/json;charset=UTF-8”,但應為“text/xml”。

來源:互聯網
上載者:User

標籤:控制台   報錯   處理   exception   代理   類型   dex   ice   資訊   

控制台對接Webservice正常,同樣的方法在Web項目上報錯:

用戶端發現響應內容類型為“application/json;charset=UTF-8”,但應為“text/xml”。
請求失敗,錯誤資訊為:
--
{
 "code" : "03010250002",
 "msg" : "Illegal json string",
 "data" : ""
}
--.

說明: 執行當前 Web 請求期間,出現未處理的例外狀況。請檢查堆疊追蹤資訊,以瞭解有關該錯誤以及代碼中導致錯誤的出處的詳細資料。 

異常詳細資料: System.InvalidOperationException: 用戶端發現響應內容類型為“application/json;charset=UTF-8”,但應為“text/xml”。
請求失敗,錯誤資訊為:
--
{
 "code" : "03010250002",
 "msg" : "Illegal json string",
 "data" : ""
}
--.

按照網上的資料找到的方法:

1、重寫代理類的請求或響應的方法

 在代理類中重寫GetWebResponse方法來設定響應的ContentType:

        protected override System.Net.WebResponse GetWebResponse(System.Net.WebRequest request)        {            var response = base.GetWebResponse(request);            if (response.ContentType == "application/json;charset=UTF-8")                response.ContentType = "text/xml";            return response;        }                protected override System.Net.WebResponse GetWebResponse(System.Net.WebRequest request, IAsyncResult result)        {            var response = base.GetWebResponse(request,result);            if (response.ContentType == "application/json;charset=UTF-8")                response.ContentType = "text/xml";            return response;        }

調試報錯:

該類未實現此屬性。

說明: 執行當前 Web 請求期間,出現未處理的例外狀況。請檢查堆疊追蹤資訊,以瞭解有關該錯誤以及代碼中導致錯誤的出處的詳細資料。 

異常詳細資料: System.NotImplementedException: 該類未實現此屬性。

源錯誤: 

行 39:             var response = base.GetWebResponse(request);行 40:             if (response.ContentType == "application/json;charset=UTF-8")行 41:                 response.ContentType = "text/xml";行 42:             return response;行 43:         }

 

重寫要求方法GetWebResponse

        protected override System.Net.WebRequest GetWebRequest(Uri uri)        {            System.Net.WebRequest request = base.GetWebRequest(uri);            request.ContentType = "text/xml";            return request;        }

結果還是報錯:用戶端發現響應內容類型為“application/json;charset=UTF-8”,但應為“text/xml”。

 

後來調試發現是URL寫錯了,因是用vs的開發命令工具產生的代理類,手動設定的URL,設定錯誤導致。

SOAPUI產生的代理類也一樣要注意URL,URL對了就正常了。

順便附上用vs的開發命令工具組建代理程式類的方法,其實就是調用了wsdl.exe 組建代理程式類

開啟vs開發控制台

wsdl /language:c# /n:命名空間 /out:D:/SOAPUI_CS/類名.cs wsdl檔案地址

 

SoapUI組建代理程式類方法

 

 

 



Webservice報錯用戶端發現響應內容類型為“application/json;charset=UTF-8”,但應為“text/xml”。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.