30

UrlEncoded形式でデータを送信する必要があるゲートウェイのデータ転送に取り組んでいます。ただし、.netのUrlEncodeは小文字のタグを作成し、転送を中断します(Javaは大文字を作成します)。

.netに大文字のUrlEncodingを強制するにはどうすればよいですか?

update1:

.net out:

dltz7UK2pzzdCWJ6QOvWXyvnIJwihPdmAioZ%2fENVuAlDQGRNCp1F

対Javaの:

dltz7UK2pzzdCWJ6QOvWXyvnIJwihPdmAioZ%2FENVuAlDQGRNCp1F

(これはbase64d 3DES文字列です。大文字と小文字を区別する必要があります)。

4

7 に答える 7

48

私はあなたがC#があなたに与えるものに固執していると思います、そしてエラーを得るのは反対側で不十分に実装されたUrlDecode関数を示唆します。

そうは言っても、文字列をループして、%記号に続く2文字だけを大文字にする必要があります。これにより、エンコードされた文字を正しい形式にマッサージしながら、base64データをそのまま維持できます。

public static string UpperCaseUrlEncode(string s)
{
  char[] temp = HttpUtility.UrlEncode(s).ToCharArray();
  for (int i = 0; i < temp.Length - 2; i++)
  {
    if (temp[i] == '%')
    {
      temp[i + 1] = char.ToUpper(temp[i + 1]);
      temp[i + 2] = char.ToUpper(temp[i + 2]);
    }
  }
  return new string(temp);
}
于 2009-05-27T21:44:41.277 に答える
26

私はこれが非常に古く、おそらくこの解決策が存在しなかったことを知っていますが、これはこれを解決しようとしたときにグーグルで見つけたトップページの1つでした。

System.Net.WebUtility.UrlEncode(posResult);

これは大文字にエンコードされます。

于 2015-10-13T08:37:45.860 に答える
15

HttpUtility.UrlEncodeの小文字のパーセントエンコーディングを正規表現に置き換えます。

static string UrlEncodeUpperCase(string value) {
    value = HttpUtility.UrlEncode(value);
    return Regex.Replace(value, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper());
}

var value = "SomeWords 123 #=/ äöü";

var encodedValue = HttpUtility.UrlEncode(value);
// SomeWords+123+%23%3d%2f+%c3%a4%c3%b6%c3%bc

var encodedValueUpperCase = UrlEncodeUpperCase(value);
// now the hex chars after % are uppercase:
// SomeWords+123+%23%3D%2F+%C3%A4%C3%B6%C3%BC
于 2009-12-16T16:52:41.973 に答える
3

これはとても簡単です

Regex.Replace( encodedString, @"%[a-f\d]{2}", m => m.Value.ToUpper() )

つまり、すべての16進文字と数字の組み合わせを大文字に置き換えます

于 2015-02-19T11:32:17.487 に答える
2

他の誰かが問題を解決するためにperlコードまたはPCRE(perl互換の正規表現)を探しにここに来た場合、urlエンコードを小文字の16進数に変換するための(可能な限り短い候補)perl式は次のとおりです。

s/%(\X{2})/%\L$1\E/go

およびその逆(小文字から大文字)

s/%(\x{2})/%\U$1\E/go
于 2013-02-07T15:52:03.660 に答える
0

これは、OAuth用のTwitterアプリケーションで使用しているコードです...

    Public Function OAuthUrlEncode(ByVal value As String) As String
    Dim result As New StringBuilder()
    Dim unreservedChars As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"
    For Each symbol As Char In value
        If unreservedChars.IndexOf(symbol) <> -1 Then
            result.Append(symbol)
        Else
            result.Append("%"c + [String].Format("{0:X2}", AscW(symbol)))
        End If
    Next

    Return result.ToString()
End Function

お役に立てれば!

于 2009-07-28T19:27:01.973 に答える
0

パブリックOAuth.OAuthBaseバージョンUrlEncode(.NET 2.0 / 3.5用)のVB.NET変換は次のとおりです。

' MEH: Made this ReadOnly because the range of unreserved characters is specified in the OAuth spec. Inheritors ought not change it.
Protected Shared ReadOnly unreservedChars As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"
' MEH: Added. These are the other characters HttpUtility.UrlEncode does not convert to percent encoding itself.
Protected Shared ReadOnly reservedChars As String = " !'()*" ' If this moves to .NET 4+ ' can be removed.


''' <summary>
''' This is a different Url Encode implementation since the default .NET one outputs the percent encoding in lower case.
''' While this is not a problem with the percent encoding spec, it is used in upper case throughout OAuth.
''' Also the OAuth spec explicitly requires some characters to be unencoded.
''' </summary>
''' <param name="Value">The value to Url encode</param>
''' <returns>Returns a Url encoded string</returns>
''' <revisionhistory>
'''   140313 MEH Fixed to correctly cater for any characters between %80 and %ff, and the O(n^2) IndexOf call.
''' </revisionhistory>
Public Shared Function UrlEncode(ByVal Value As String) As String
    Dim result, buffer As New StringBuilder()

    For Each symbol As Char In Value
        If unreservedChars.IndexOf(symbol) <> -1 Then
            UrlEncodeAppendClear(result, buffer).Append(symbol)
        ElseIf reservedChars.IndexOf(symbol) = -1 Then
            'some symbols produce 2 or more octets in UTF8 so the system urlencoder must be used to get the correct data
            ' but this is best done over a full sequence of characters, so just store this one in a buffer.
            buffer.Append(symbol)
        Else ' These characters are not converted to percent encoding by UrlEncode.
            UrlEncodeAppendClear(result, buffer).AppendFormat(Globalization.CultureInfo.InvariantCulture, "%{0:X2}", AscW(symbol))
        End If
    Next
    UrlEncodeAppendClear(result, buffer)
    Return result.ToString()
End Function


Private Shared percentHex As New RegularExpressions.Regex("(%[0-9a-f][0-9a-f])", RegularExpressions.RegexOptions.Compiled)

''' <summary>
'''  Actually performs the UrlEncode on any buffered characters, ensuring the resulting percents are uppercased and clears the buffer.
''' </summary>
''' 
''' <param name="Result">The result of the UrlEncode is appended here.</param>
''' <param name="Buffer">The current buffer of characters to be encoded. Cleared on return.</param>
''' 
''' <returns>The <paramref name="Result">Result</paramref>, as passed in, with the UrlEncoding of the <paramref name="Buffer">buffer of characters</paramref> appended.</returns>
''' 
''' <remarks>Would like to be an extension method.</remarks>
''' 
''' <revisionhistory>
'''   140313 MEH Created.
''' </revisionhistory>
Private Shared Function UrlEncodeAppendClear(ByVal Result As StringBuilder, ByVal Buffer As StringBuilder) As StringBuilder
    If Buffer.Length > 0 Then
        Result.Append(percentHex.Replace(HttpUtility.UrlEncode(Buffer.ToString), Function(c) c.Value.ToUpperInvariant()))
        Buffer.Length = 0
    End If
    Return Result
End Function
于 2014-09-03T10:47:30.160 に答える