Pregunta

Related: DotNetOpenAuth I Need Send Long string in FetchResponse and OpenId query length issue in DotNetOpenAuth? - but neither of these are able to answer my question satisfactorily.

We are using DotNetOpenAuth to post data to Xero, which supports a maximum of 3MB per request. We are trying to post a 77Kb XML string in requestBody:

Dictionary<string, string> additionalParams = new Dictionary<string, string>();
additionalParams.Add("xml",requestBody);

var endpoint = new MessageReceivingEndpoint(requestURL, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest);
HttpWebRequest request = XeroConsumer.PrepareAuthorizedRequest(endpoint, accessToken, additionalParams);

WebResponse response = request.GetResponse();
string thisResponse = (new StreamReader(response.GetResponseStream())).ReadToEnd();

PrepareAuthorizedRequest is throwing: Invalid URI: The Uri string is too long.

Is there any way I can POST "large" data with DotNetOpenAuth?

¿Fue útil?

Solución

DotNetOpenAuth uses Uri.HexEscape() and Uri.EscapeDataString() on the additional parameters. This breaks when your parameters are over 2Kb in length.

I have swapped out their function for my own inefficient, but working, function:

    internal static string EscapeUriDataStringRfc3986(string value)
    {
        StringBuilder escaped = new StringBuilder();

        string validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~";

        foreach (char c in value)
        {
            if(validChars.Contains(c.ToString())){
                escaped.Append(c);
            } else {
                escaped.Append("%" + Convert.ToByte(c).ToString("x2").ToUpper());
            }
        }

        // Return the fully-RFC3986-escaped string.
        return escaped.ToString();
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top