none
英文版C#商业软件软件显示中文出现乱码,请帮助分析原因 RRS feed

  • 问题

  • 老师们好:

        最近在使用丹麦一家公司的英文版商业软件,该软件声称是基于.net  C#开发的。该软件的工作流程大致如下:客户端发送XML文件到服务器端,服务器对XML进行解码,提取信息显示。我使用该软件自带的测试代码,现象是:发送的XML中都是英文信息,服务器端可以正确显示,如果XML中包含中文信息,则服务器端对应的部分显示“??”(不包含引号)。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using VideoOS.Common.AlertAPI;
    using System.Net;
    using System.Net.Sockets;
    
    namespace AlertSample
    {
      class AlertSampleProgram
      {
        /// <summary>
        /// Sample program that creates an alert and sends data to a generic driver on the network
        /// </summary>
        /// <param name="args"></param>
        static void Main( string[] args )
    		{
    			string host = "127.0.0.1"; // The host to which the alert is sent
    			int port = 9090;      // The port on which the alert is sent
    
    
    			// Create new alert
    			Alert alert = new Alert();
    
    			alert.Name = "Hallway_Tripwire";
    			alert.Description = "Tripwire at main door";
    			alert.Type = 3;
    			alert.TimeStamp = DateTime.Now;
    
    			// Create source
    			alert.Source = new AlertSource();
    
    			// Create server
    			alert.Source.Server = new AlertSourceServer();
    			alert.Source.Server.Description = "Milestone Surveillance Server";
    			alert.Source.Server.GUID = "CCCF8C51-94DB-49b2-9718-0800A151D76B";
    			alert.Source.Server.Hostname = "127.0.0.1";
    
    			// Create device
    			alert.Source.Device = new AlertSourceDevice();
    			alert.Source.Device.Name = "[Axis 221] Camera 1";
    			alert.Source.Device.GUID = "D23414E5-B03F-4C78-8AB9-A36D1488F9C2";
    			alert.Source.Device.Hostname = "0.0.0.1";
    
    			// Create location
    			alert.Source.Device.Location = new AlertSourceDeviceLocation();
    			alert.Source.Device.Location.Name = "Hallway Corner";
    
    			// Create rule info
    			alert.Rule = new AlertRule();
    			alert.Rule.Name = "Hallway_Tripwire";
    			alert.Rule.Type = "Tripwire";
    
    			// Add rule polygon
    			alert.Rule.Polygon = new TPolygon();
    
    			double[,] rulePoints = { { 241.0, 63.0 }, { 65.0, 140.0 }, { 75.0, 284.0 },
    				{ 115.0, 402.0 }, { 238.0, 448.0 }, { 398.0, 462.0 }, { 521.0, 374.0 },
    				{ 494.0, 235.0 }, { 486.0, 63.0 }, { 303.0, 37.0 } };
    
    			for ( int i = 0; i < rulePoints.GetLength( 0 ); i++ )
    			{
    				alert.Rule.Polygon.AddPoint( new TPoint( rulePoints[ i, 0 ], rulePoints[ i, 1 ] ) );
    			}
    
    			// Add rule polygon color
    			alert.Rule.Polygon.Color = new TColor( 0, 0, 255, 255 );
    
    			// Create object
    			AlertObject obj = new AlertObject();
    			obj.ID = 42;
    			obj.Type = "Person";
    			obj.Size = 2.39;
    			obj.Name = "John Doe";
    			obj.Confidence = 998;
    			obj.BoundingBox = new AlertObjectBoundingBox( 194.0, 286.0, 429.0, 408.0 );
    			obj.BoundingBox.Color = new TColor( 255, 0, 0, 255 );
    
    			obj.Motion = new AlertObjectMotion();
    			obj.Motion.Speed = 1.816466;
    			obj.Motion.ScreenSpeed = 0.2389245;
    			obj.Motion.SpeedAngle = 1.546658;
    
    			// add object path
    			obj.Motion.Path = new TPolygon();
    
    			double[,] motionPoints = { { 88.0, 288.0 }, { 129.0, 300.0 }, { 150.0, 304.0 },
    				{ 169.0, 307.0 }, { 186.0, 311.0 }, { 201.0, 313.0 }, { 215.0, 315.0 },
    				{ 229.0, 316.0 }, { 242.0, 316.0 }, { 255.0, 316.0 }, { 262.0, 316.0 },
    				{ 272.0, 315.0 }, { 284.0, 314.0 }, { 296.0, 312.0 }, { 307.0, 310.0 },
    				{ 318.0, 309.0 }, { 327.0, 308.0 }, { 332.0, 308.0 } };
    
    			for ( int i = 0; i < motionPoints.GetLength( 0 ); i++ )
    			{
    				obj.Motion.Path.AddPoint( new TPoint( motionPoints[ i, 0 ], motionPoints[ i, 1 ] ) );
    			}
    
    			obj.Motion.Path.Color = new TColor( 0, 255, 0, 255 );
    
    			alert.AddObject( obj );
    
    			// Vendor
    			alert.Vendor = new AlertVendor();
    			alert.Vendor.Name = "VCA Vendor";
    
    			Console.WriteLine( "Sample alert created." );
    
    			AlertSerializer serializer = new AlertSerializer();
    
    			// Send to generic driver
    			Socket socket = null;
    
    			try
    			{
    				string alertXml = serializer.Serialize( alert );
    				Console.WriteLine( "Alert serialized." );
    
    				IPAddress hostIPAddress = IPAddress.Parse( host );
    				IPEndPoint hostIPEndPoint = new IPEndPoint( hostIPAddress, port );
    				socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
    
    				// Connect to remote host
    				socket.Connect( hostIPEndPoint );
    				socket.Send( Encoding.ASCII.GetBytes( alertXml ) );
    				socket.Close();
    
    				Console.WriteLine( "Alert sent to host: " + host + ", port: " + port.ToString() );
    
    			}
    			catch ( Exception e )
    			{
    				Console.WriteLine( e.InnerException.Message );
    
    				if ( socket != null )
    				{
    					socket.Close();
    				}
    			}
    
    			Console.WriteLine( "\nPress enter to continue..." );
    			Console.Read();
    		}
      }
    }
    
    

     

    因为我是C#新手,感觉问题应该出在服务器端的字符解析模块,通过客户端的字符适当编码应该可以使服务器正确识别中文,但具体如何实现?请各位老师帮忙,谢谢!
    2011年7月29日 12:32

答案

  • Hi~

    是否有考慮到編碼問題 ( Encoding.UTF8...... ) ,看可不可以規範統一編碼方式,這樣就可以與中文字相容。


    逐步學習,逐夢踏實;腳步要踩穩,這樣下一步才不會跌倒。 http://www.dotblogs.com.tw/nobel12
    2011年7月30日 9:14

全部回复