编程语言
首页 > 编程语言> > c# – 如何将base64发布到Webservice

c# – 如何将base64发布到Webservice

作者:互联网

我想POST一个参数,它是base64到webservice,但它不起作用.

    string postData = "base64=hySGNg22yX0AIjNQvNJw0xHEbi32NH+iRVKDkbOmUiKpxW0/S6rUgfLmSa527Y8zZ3GmEzfXMB8ry8fH5GjGeJFGqFxDC4A2hLlvIFzyiDeUCqloGDAJoOLa8Jnif1C8nwRte0GNCRM5RXkshLKZP2itZclgDCYz6OgAF7oLNluoB44cZ3AgF1Q8hVgME8LeqgOqxsIzTt3Xn+96j7zp/wA=";
    byte[] dataArray = Encoding.Default.GetBytes(postData);

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage);
    request.Method = "POST";
    request.ContentLength = dataArray.Length;
    request.ContentType = "application/x-www-form-urlencoded";

    Stream dataStream = null;
    try
    {
        dataStream = request.GetRequestStream();
    }
    catch (Exception)
    {
    }

    dataStream.Write(dataArray, 0, dataArray.Length);
    dataStream.Close();

但requestHeader中的参数更改为:

BASE64 = hySGNg22yX0AIjNQvNJw0xHEbi32NH + iRVKDkbOmUiKpxW0 / S6rUgfLmSa527Y8zZ3GmEzfXMB8ry8fH5GjGeJFGqFxDC4A2hLlvIFzyiDeUCqloGDAJoOLa8Jnif1C8nwRte0GNCRM5RXkshLKZP2itZclgDCYz6OgAF7oLNluoB44cZ3AgF1Q8hVgME8LeqgOqxsIzTt3Xn + 96j7zp / WA =

所以WebService收到一个错误的参数:

[WebMethod]
public string GenerateOrder(string base64)
{ 
    byte[] buffer = Convert.FromBase64String(base64); //Exception here
} 

解决方法:

使用HttpUtility.UrlDecode(base64),然后从base64转换回来.

标签:c,web-services,base64,webmethod
来源: https://codeday.me/bug/20190629/1326025.html