【问题标题】:Exception converting Java byte to VB.NET byte (WebService)将 Java 字节转换为 VB.NET 字节(WebService)的异常
【发布时间】:2011-12-10 03:54:00
【问题描述】:

我正在尝试使用 Java 1.5 从 Android 2.3 应用程序发送 JSON 字符串,转换为 byte[] 到等待 POST Base64Binary 流的 ASP.NET WebService 方法。

这是我对 JSON 字符串进行编码的 Java 代码:

String encoded = Base64.encodeToString(me.getValue().getBytes(), Base64.DEFAULT);

me 是我的 JSON 字符串。

这是我从 ASP.NET 得到的错误:

System.ArgumentException: Cannot convert eyJJZEV0YXRJbnNwZWN0aW9uIjoiMSIsIkFwcGxpY2FibGUiOiJUcnVlIiwiSWRFdGF0IjoiMSIsIklkVGFzayI6IjczOCIsIkRhdGVEZXJuaWVyTW9kaWYiOiIyMDExLTEyLTA5IDIwOjA5OjIyIiwiSWRDb250cmF0IjoiMzg1NTYiLCJJZFRhc2tDb250cmF0IjoiNDc5ODExMSJ9 to System.Byte.
 Parameter name: type ---> System.FormatException: Input string was not in a correct format.
    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
    at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
    at System.Byte.Parse(String s, NumberStyles style, NumberFormatInfo info)
    at System.String.System.IConvertible.ToByte(IFormatProvider provider)
    at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
    at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
    --- End of inner exception stack trace ---
    at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
    at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
    at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
    at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
    at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

知道为什么会出现此错误(我无法控制 .NET 端)吗?

谢谢! 尼古拉斯。

【问题讨论】:

    标签: java android .net web-services


    【解决方案1】:

    我之前也遇到过类似的问题。我在 Android 上实现了一个代码来使用 Base64 进行 SHA-1 散列。

    我还在 .NET 中的 Web 服务方法中在服务器端进行 SHA-1 哈希处理

    这是我的服务器端代码的样子(.asmx 网络服务)

       public class Service1 : System.Web.Services.WebService
          { 
                [WebMethod]
              public string HashCode(string str)
           {
              string rethash = "";
           try
             {
    
            System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
            System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
            byte[] combined = encoder.GetBytes(str);
            hash.ComputeHash(combined);
            rethash = Convert.ToBase64String(hash.Hash);
              }
          catch (Exception ex)
            {
            string strerr = "Error in HashCode : " + ex.Message;
            }
        return rethash;
         }
      }
    

    这对我有用,我可以正确计算我的字节数组的哈希值。

    希望这能给你一些想法,

    干杯

    一切顺利

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 2011-03-11
      • 2011-01-22
      • 1970-01-01
      相关资源
      最近更新 更多