簡體   English   中英

Base64字符串發布到URL

[英]Base64 String posting to URL

我有一些字符串,需要將其字符串發布到url之前,必須將其轉換為base64。 這是我的代碼

  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
      byte[] postDataBytes = Encoding.UTF8.GetBytes(strCXML);
       string returnValue = System.Convert.ToBase64String(postDataBytes);
       req.Method = "POST";
       req.ContentLength = postDataBytes.Length;
       req.ContentLength = postDataBytes.Length;
       Stream requestStream = req.GetRequestStream();
       requestStream.Write(returnValue,0, postDataBytes.Length);

問題是我在最后一行得到錯誤System.IO.Stream.Write(byte [],int,int)returnValue是base64字符串不能用作stream.writer中需要的byte []知道如何使用該base64字符串叫做returnvalue,並把它放在一個URL,謝謝

您應該通過Encoding.GetBytes將base64字符串轉換為字節數組。

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
byte[] postDataBytes = Encoding.UTF8.GetBytes(strCXML);
string returnValue = System.Convert.ToBase64String(postDataBytes);

postDataBytes = Encoding.UTF8.GetBytes(returnValue);

req.Method = "POST";
req.ContentLength = postDataBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postDataBytes, 0, postDataBytes.Length);

您正在使用Stream作為您的requestStream。 Stream.Write需要一個字節數組,而不是Base64字符串。

我認為您執行順序錯誤。 我先將strCXML轉換為Base64字符串,然后將其編碼為字節數組以寫入請求流。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM