如何从 .NET WebClient 反序列化包含 HTTP POST 参数的字节数组
How to deserialise a byte array containing HTTP POST parameters from a .NET WebClient
我正在使用 WebClient 向远程 URL 发送 HTTP POST 请求,如下所示:
byte[] responsebytes = client.UploadValues(
"https://www.jaja.com/yadayadayada", "POST", reqparm);
responsebody = Encoding.UTF8.GetString(responsebytes);
URL 以 HTTP POST 参数的形式给出响应,例如:
"Status=Ok&BrowserUrl=http%3a%2f%2fwww.jaja.com%3a7106%2fxxx&Hash=8614C21DD93749339906DB35C51B06006B33DC8C192F40DFE2DB6549942C837C4452E1D1333DE9DB7814B278C8B9E3C34D1A76D2F937DEE57502336E0A071412"
我在这里面临的问题是如何将此响应序列化到一个对象 class:
public class PaynowResponseModel : PayNowBase
{
public string browserurl { get; set; }
public string pollurl { get; set; }
public string status { get; set; }
public string hash { get; set; }
}
我试过下面的方法
public static object ByteArrayToObject(Byte[] buffer)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream(buffer);
object rval = formatter.Deserialize(stream);
stream.Close();
return rval;
}
我是这样称呼的:
var responseObj = (PaynowResponseModel) General.ByteArrayToObject(responsebytes);
但它不起作用。我在 ByteArrayToObject 方法
中遇到异常
System.Runtime.Serialization.SerializationException was caught
HResult=-2146233076 Message=The input stream is not a valid binary
format. The starting contents (in bytes) are:
73-74-61-74-75-73-3D-4F-6B-26-62-72-6F-77-73-65-72 ...
Source=mscorlib StackTrace:
at System.Runtime.Serialization.Formatters.Binary.SerializationHeaderRecord.Read(__BinaryParser
input)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadSerializationHeaderRecord()
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler
handler, __BinaryParser serParser, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream
serializationStream, HeaderHandler handler, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream
serializationStream)
请帮助我如何解决这个问题或实现我的目标的替代方法。
提前谢谢你:)
您不能直接反序列化字节数组,因为字节数组不是有效的 PayNowResponseModel
序列化对象。
使用 HttpUtility.ParseQueryString(Encoding.UTF8.GetString(responsebytes))
将检索到的字符串解析为 NameValueCollection,然后创建一个转换器将数据转换为 PayNowResponseModel
对象。
[您可能需要添加对 System.Web]
的引用
看看下面的示例:
var resp = HttpUtility.ParseQueryString(responsebytes);
var converted = new PaynowResponseModel();
converted.browserurl = resp["BrowserUrl"];
converted.status = resp["Status"];
converted.hash = resp["Hash"];
我正在使用 WebClient 向远程 URL 发送 HTTP POST 请求,如下所示:
byte[] responsebytes = client.UploadValues(
"https://www.jaja.com/yadayadayada", "POST", reqparm);
responsebody = Encoding.UTF8.GetString(responsebytes);
URL 以 HTTP POST 参数的形式给出响应,例如:
"Status=Ok&BrowserUrl=http%3a%2f%2fwww.jaja.com%3a7106%2fxxx&Hash=8614C21DD93749339906DB35C51B06006B33DC8C192F40DFE2DB6549942C837C4452E1D1333DE9DB7814B278C8B9E3C34D1A76D2F937DEE57502336E0A071412"
我在这里面临的问题是如何将此响应序列化到一个对象 class:
public class PaynowResponseModel : PayNowBase
{
public string browserurl { get; set; }
public string pollurl { get; set; }
public string status { get; set; }
public string hash { get; set; }
}
我试过下面的方法
public static object ByteArrayToObject(Byte[] buffer)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream(buffer);
object rval = formatter.Deserialize(stream);
stream.Close();
return rval;
}
我是这样称呼的:
var responseObj = (PaynowResponseModel) General.ByteArrayToObject(responsebytes);
但它不起作用。我在 ByteArrayToObject 方法
中遇到异常System.Runtime.Serialization.SerializationException was caught
HResult=-2146233076 Message=The input stream is not a valid binary format. The starting contents (in bytes) are: 73-74-61-74-75-73-3D-4F-6B-26-62-72-6F-77-73-65-72 ...
Source=mscorlib StackTrace: at System.Runtime.Serialization.Formatters.Binary.SerializationHeaderRecord.Read(__BinaryParser input) at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadSerializationHeaderRecord() at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
请帮助我如何解决这个问题或实现我的目标的替代方法。
提前谢谢你:)
您不能直接反序列化字节数组,因为字节数组不是有效的 PayNowResponseModel
序列化对象。
使用 HttpUtility.ParseQueryString(Encoding.UTF8.GetString(responsebytes))
将检索到的字符串解析为 NameValueCollection,然后创建一个转换器将数据转换为 PayNowResponseModel
对象。
[您可能需要添加对 System.Web]
看看下面的示例:
var resp = HttpUtility.ParseQueryString(responsebytes);
var converted = new PaynowResponseModel();
converted.browserurl = resp["BrowserUrl"];
converted.status = resp["Status"];
converted.hash = resp["Hash"];