1

I am sending an HTTP POST request to a remote URL using WebClient as follows:

byte[] responsebytes = client.UploadValues(
                        "https://www.jaja.com/yadayadayada", "POST", reqparm);
responsebody = Encoding.UTF8.GetString(responsebytes);

The URL gives a response in the form of HTTP POST parameters e.g:

"Status=Ok&BrowserUrl=http%3a%2f%2fwww.jaja.com%3a7106%2fxxx&Hash=8614C21DD93749339906DB35C51B06006B33DC8C192F40DFE2DB6549942C837C4452E1D1333DE9DB7814B278C8B9E3C34D1A76D2F937DEE57502336E0A071412"

The problem I'm facing here is how do I serialise this response to an object for this 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; }
    }

I have tried the following method

public static object ByteArrayToObject(Byte[] buffer)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream(buffer);
            object rval = formatter.Deserialize(stream);
            stream.Close();
            return rval;
        }

Which I'm calling this way:

var responseObj = (PaynowResponseModel) General.ByteArrayToObject(responsebytes);

But it's not working. I get an exception in the ByteArrayToObject method

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)

Please assist with how I can solve this problem or an alternative way of achieving my goal there.

Thank you in advance :)

1 Answer 1

1

You can't directly deserialize the byte array since the byte array is not a valid PayNowResponseModel serialized object.

Use HttpUtility.ParseQueryString(Encoding.UTF8.GetString(responsebytes)) in order to parse the retrieved string to a NameValueCollection, then create a converter to convert the data to PayNowResponseModel object. [You might need to add a reference to System.Web]

Take a look at the following sample:

        var resp = HttpUtility.ParseQueryString(responsebytes);
        var converted = new PaynowResponseModel();
        converted.browserurl = resp["BrowserUrl"];
        converted.status = resp["Status"];
        converted.hash = resp["Hash"];
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome this is the perfect solution. Simple and to the point. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.