2011-12-10 39 views
3

我试图从Android 2.3应用程序使用Java 1.5将JSON字符串转换为一个字节[]转换为ASP.NET WebService方法等待POST Base64Binary流。将Java字节转换为VB.NET字节(WebService)的异常

这是我的Java代码来编码JSON字符串:

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

我是我的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方面)?

谢谢! Nicolas。

回答

2

我以前遇到过类似的问题。我在Android上实现了一个代码,使用Base64完成SHA-1散列。

我也做SHA-1散列在服务器端在我的web服务方法在.NET

这里是我的服务器端代码如下所示(的.asmx Web服务)

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

这为我工作,我可以正确计算我的字节数组的散列。

希望这给你一些想法,

干杯

所有最优秀的

+0

谢谢您的解决方案。你说得对。 –