2012-04-05 8 views
2

"+"記号が白に変換されたbase64文字列を受け取った場合、C#.onサーバー側でHttpWebRequestを使用してアップロードする画像をbase64文字列に変換します。スペース ""。このbase64文字列をbyte array.iに変換する際にエラーが出るので、サーバーサイドで(Webサービスで)変更する必要はありません。私はこの問題をクライアント側で解決したいと思います。クライアント側のコードは次のとおりです。C#でHttpWebRequestを使用してbase64文字列を送信する "+"空白に変換する

//////////////////

WSManagerResult wsResult =新しいWSManagerResult()。

 try 
     { 
      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(serviceURL); 
      req.Method = "POST"; 
      req.ProtocolVersion = HttpVersion.Version11; 
      req.ContentType = "application/x-www-form-urlencoded"; 
      // req.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; 
      // req.CookieContainer = new CookieContainer(); 


      string content = string.Empty; 
      foreach (KeyValuePair<string, string> entry in paramDic) 
      { 

// entry.Valueは上記base64文字列のすべての「+」符号を引き起こすspaces.which白「」に変換された画像

   content = content + entry.Key + "=" + entry.Value + "&&"; 
      } 
      content = content.TrimEnd('&'); // input parameter if u have more that one //a=b&dd=aa    
      req.ContentLength = content.Length; 
      // req = URLEncode(content); 
      Stream wri = req.GetRequestStream(); 


      byte[] array = Encoding.ASCII.GetBytes(content); 
      if (array.Length > 0) 
       wri.Write(array, 0, array.Length); 
      wri.Flush(); 
      wri.Close(); 
      WebResponse rsp = (HttpWebResponse)req.GetResponse(); 


      byte[] b = null; 
      using (Stream stream = rsp.GetResponseStream()) 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       int count = 0; 
       do 
       { 
        byte[] buf = new byte[1024]; 
        count = stream.Read(buf, 0, 1024); 
        ms.Write(buf, 0, count); 
       } while (stream.CanRead && count > 0); 
       b = ms.ToArray(); 
      } 
      wsResult.result = Encoding.ASCII.GetString(b); 
     } 
     catch (Exception e) 
     { 
      clsException.ExceptionInstance.HandleException(e); 
      wsResult.error = e.Message; 
     } 


     return wsResult; 

から定格ベース64ストリング遺伝子であります上記のような問題。

この問題を解決するのを手伝ってください。

よろしく

シャーハリドは

+1

質問する前に私の質問(コード/入力)を最小限に抑えようと思っています...今、私はそれが非常に重要で良いことであることを認識しています。 –

答えて

2

はちょうどそのために設計されていますBase64Url encodeと呼ばれるエンコーディングが存在します。ただし、エンコード/デコードをそれぞれの端で行う必要があります。 Base64のURLで

+0

多くのおかげです。Rob.itは、クライアント側で16進数の '%2B'に '+'を置き換えて、C#で以下のwire.asを介してデータをポストすることで問題を解決しました。/* URLに標準のBase64を使用する場合は、 '+'、 '/'、 '='文字を特殊文字でエンコードされた16進シーケンス( '+' = '%2B'、 '/' =%2F ' = '='%3D ')* /文字列stbase64datatopost = stbase64datatopost.Replace( "+"、@ "%2B"); stbase64datatopost = stbase64datatopost .Replace( "/"、@ "%2F"); stbase64datatopost = stbase64datatopost.Replace( "="、@ "%3D"); – shahkhalid

1

を、それが安全にスペースのようなすごみを追加し、標準のURLエンコーダなしのワイヤ上を通過することができるように、それは_-から+/を変換し、またはパーセント-進エンコードされたあなたのbase64を投稿してくださいHttpUtility.UrlEncode(entry)

WebサービスとWebサービスにデータがRob.itがにそれに「+」、クライアント側で進数は「%2B」に置き換えることで、私の問題を解決し、コード

3

多くの感謝を変更することなく、それを解析することができすべきp ostデータをwire.as以下のC#で。

/*Using standard Base64 in URL requires encoding of '+', '/' and '=' characters into special percent-encoded hexadecimal sequences ('+' = '%2B', '/' = '%2F' and '=' = '%3D')*/ 

String stbase64datatopost =stbase64datatopost.Replace("+",@"%2B"); 
stbase64datatopost = stbase64datatopost .Replace("/",@"%2F");  
stbase64datatopost=stbase64datatopost.Replace("=",@"%3D"); 
+0

これは私のために働いた。私の場合、+は空白で置き換えられ、/または=は改行で置き換えられました。 – kannankeril

関連する問題