gpt4 book ai didi

java - 将 Java 字节转换为 VB.NET 字节的异常 (WebService)

转载 作者:太空狗 更新时间:2023-10-29 13:38:20 25 4
gpt4 key购买 nike

我正在尝试从使用 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 端)吗?

谢谢!尼古拉斯。

最佳答案

我之前遇到过类似的问题。我已经在 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;
}
}

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

希望这能给你一些想法,

干杯

一切顺利

关于java - 将 Java 字节转换为 VB.NET 字节的异常 (WebService),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8454258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com