這個base64在requestHeader里面變成了
base64=hySGNg22yX0AIjNQvNJw0xHEbi32NH%2BiRVKDkbOmUiKpxW0%2FS6rUgfLmSa527Y8zZ3GmEzfXMB8ry8fH5GjGeJFGqFxDC4A2hLlvIFzyiDeUCqloGDAJoOLa8Jnif1C8nwRte0GNCRM5RXkshLKZP2itZclgDCYz6OgAF7oLNluoB44cZ3AgF1Q8hVgME8LeqgOqxsIzTt3Xn%2B96j7zp%2FwA%3D
他把base64里面一些/和= 給轉成%2F之類的東西了。
所以webservice接收到的參數是錯的。。
解決辦法:
string base64 = "base64=hySGNg22yX0AIjNQvNJw0xHEbi32NH+iRVKDkbOmUiKpxW0/S6rUgfLmSa527Y8zZ3GmEzfXMB8ry8fH5GjGeJFGqFxDC4A2hLlvIFzyiDeUCqloGDAJoOLa8Jnif1C8nwRte0GNCRM5RXkshLKZP2itZclgDCYz6OgAF7oLNluoB44cZ3AgF1Q8hVgME8LeqgOqxsIzTt3Xn+96j7zp/wA="; byte[] buffer = Convert.FromBase64String(base64); string Base64StrData = Convert.ToBase64String(buffer).Replace("+", "%2B");//注意加號(’+‘)的替換處理,否則由於加號經過Url傳遞后變成空格而得不到合法的Base64字符串 string postData = "str=" + Base64StrData; byte[] dataArray = Encoding.Default.GetBytes(postData);
/// <summary> /// POST請求與獲取結果 /// </summary> public static string HttpPost(string Url, string postDataStr) { try { postDataStr = postDataStr.Replace("+", "%2B"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.Timeout = 6000000; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postDataStr.Length; StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII); writer.Write(postDataStr); writer.Flush(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentEncoding; if (encoding == null || encoding.Length < 1) { encoding = "UTF-8"; //默認編碼 } StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)); string retString = reader.ReadToEnd(); return retString; } catch (Exception ex) { return null; } }
