简体   繁体   中英

Exception converting Java byte to VB.NET byte (WebService)

I'm trying to send a JSON string, from an Android 2.3 application, using Java 1.5, converted into a byte[] to an ASP.NET WebService method that is waiting for a POST Base64Binary stream.

Here is my Java code to encode the JSON string :

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

me is my JSON string.

Here is the error I'm getting from 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()

Any idea why I'm getting this error (I can't control the .NET side) ?

Thanks! Nicolas.

I faced a similar problem before. I had implemented a code on Android to do SHA-1 hashing using Base64.

I was also doing SHA-1 hashing on server side in my web service method in .NET

Here is what my server side code looks like (.asmx web service)

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

This worked for me and I could compute the hash of my byte array properly.

Hope this gives you some idea,

Cheers

All the best

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM