Frage

Ich arbeite an einer Datenübertragung für ein Gateway, das mich erfordert Daten in UrlEncoded Form zu senden. Allerdings .net des UrlEncode erzeugt Klein Tags, und es bricht die Übertragung (Java erstellt Groß).

Alle Gedanken, wie ich .net Groß urlencoding dazu zwingen kann?

update1:

.net aus:

dltz7UK2pzzdCWJ6QOvWXyvnIJwihPdmAioZ%2fENVuAlDQGRNCp1F

vs Java:

dltz7UK2pzzdCWJ6QOvWXyvnIJwihPdmAioZ%2FENVuAlDQGRNCp1F

(es wird ein base64d 3DES-String, ich brauche es ist Fall zu halten).

War es hilfreich?

Lösung

Ich glaube, ich ist stecken mit dem, was C # gibt Ihnen und Fehler immer schlägt eine schlecht umgesetzt urldecode Funktion am anderen Ende.

Wenn das gesagt ist, sollten Sie nur durch die Zeichenfolge Schleife benötigen und Großbuchstaben nur die beiden Zeichen nach einem% Zeichen. Das wird Ihre base64 Daten intakt halten, während die codierten Zeichen in das richtige Format zu massieren:

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);
}

Andere Tipps

Ich weiß, das ist sehr alt und vielleicht hat diese Lösung nicht existieren, aber dies war eine der Top-Seiten, die ich auf Google gefunden, wenn diese zu lösen versuchen.

System.Net.WebUtility.UrlEncode(posResult);

Dieses kodiert in Groß.

Ersetzen Sie die Klein Prozent Codierung von HttpUtility.UrlEnocde mit einem Regex:

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

Dies ist sehr einfach

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

d. Ersetzen Sie alle hex Buchstaben-Ziffern-Kombinationen in Großbuchstaben

, ob jemand hier auf der Suche wird für Perl-Code oder ein PCRE (Perl-kompatible reguläre Ausdrücke), um das Problem zu lösen, wird ein (Kandidat für die kürzestmögliche) Perl-Ausdruck URL-Kodierung in Kleinbuchstaben hex zu konvertieren ist:

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

und umgekehrt (Kleinbuchstaben in Großbuchstaben)

s/%(\x{2})/%\U$1\E/go

Dies ist der Code, den ich für OAuth ...

in einer Twitter-Anwendung
    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

Hope, das hilft!

Hier ist meine VB.NET Umwandlung der öffentlichen OAuth.OAuthBase Version UrlEncode (für .NET 2.0 / 3.5):

' 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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top