在创建POST请求时处理字符串中的特殊字符c#

时间:2016-02-17 15:24:03

标签: c# httprequest special-characters

我有一个字符串,例如CommentText = " This i$ @ b@d $+rin&"。我想在创建帖子请求时包含它。我做了这样的事情: -

public async Task<string> PostComment(string hostUrl, string accountName, string key, string newsSourceId, string articleGuid, string newsId, string commentText, string authorDisplayName)
{
    string url = EndPoints.PostCommentsURL;
    bool isCommented = false;
    string paramString = "HostUrl=" + hostUrl + "&AccountName=" + accountName + "&Key=" + key + "&newsSourceId=" + newsSourceId + "&articleGuid=" + articleGuid + "&articleId=" + newsId + "&commentText=" + commentText + "&authorDisplayName=" + authorDisplayName;
    var content = new StringContent(paramString, Encoding.UTF8, "application/json");
    content.Headers.Clear();
    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    HttpClientHandler handler = new HttpClientHandler();
    HttpClient httpClient = new HttpClient(handler);

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);

    try
    {
        HttpResponseMessage response = await httpClient.PostAsync(url, content);
        switch (response.StatusCode)
        {
            case HttpStatusCode.OK:
                receiveStream = await response.Content.ReadAsStringAsync();
                break;
            case HttpStatusCode.Forbidden:
                receiveStream = "UserInvalid";
                break;
            default:
                receiveStream = null;
                break;
        }

    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.WriteLine(e.Message.ToString());
    }
    return receiveStream;
}

我的问题是,在发送请求时,只有特殊字符前的字符串才能进入服务器,并且无法看到特殊字符。如何将这些字符作为字符串的一部分包含在内。

0 个答案:

没有答案