简体   繁体   English

HTTPWebResponse在参数中传递“特殊字符组合”作为密码时,无法流式传输响应

[英]HTTPWebResponse Failed to stream the response when passing “Combination Of Special characters” as password in Parameters

Working Scenario 工作场景

When Passing first parameter "strPostData" (Not contain special characters in password) as below xml request means works fine and StreamReader providing the response as well & variable "strResult" loaded successfully.. "Request=janajana" 当按如下方式传递第一个参数“ strPostData”(密码中不包含特殊字符)时,xml请求意味着工作正常,StreamReader也提供了响应,并且成功加载了变量“ strResult”。“ Request = janajana”

NOT Working Scenario 不工作的情况

But when user password contains "special characters" in first parameter "strPostData" means, StreamReader failed to provide the response & variable "strResult" not loaded successfully.. "Request=<request><Username>jana</Username><Password>jana!@#$%^&*()</Password></request>" When passing password with combination of "special characters" when HTTPWebResponse failed & Streamreader also gets failed.. 但是,当用户密码的第一个参数“ strPostData”中包含“特殊字符”时,StreamReader无法提供未成功加载的响应和变量“ strResult”。. "Request=<request><Username>jana</Username><Password>jana!@#$%^&*()</Password></request>"当HTTPWebResponse失败并且Streamreader也失败时,通过“特殊字符”组合传递密码。

I have tried with Stream object statement with .. 我已经尝试使用..与Stream对象声明

//string tempString = Encoding.UTF8.GetString(buffer, 0, buffer.Length); & 

Read Buffer code change //Int32 count = await streamRead.ReadAsync(readBuffer, 0, 256); 读取缓冲区代码更改// // Int32 count =等待streamRead.ReadAsync(readBuffer,0,256); but not works ,,, please guide me anyone 但不起作用,,,请任何人指导我

public string[] GetResponseWebAPI(string strPostData, string strUrl)
{            
string[] arrReturn = new string[3];
HttpWebResponse myHttpWebResponse = null;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] buffer = encoding.GetBytes(strPostData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myRequest.Timeout = 25000;// 25s
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = buffer.Length;
myRequest.AllowAutoRedirect = true;
ServicePointManager.ServerCertificateValidationCallback = new 
System.Net.Security.RemoteCertificateValidationCallback
(AcceptAllCertifications);
Stream newStream = myRequest.GetRequestStream();
newStream.Write(buffer, 0, buffer.Length);
newStream.Close();
myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse();
WebHeaderCollection webHeader = myHttpWebResponse.Headers;
arrReturn[0] = webHeader["Statuscode"];
arrReturn[1] = webHeader["Statusmessage"];                
Stream streamResponse = myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
Char[] readBuffer = new Char[256];
int count = streamRead.Read(readBuffer,0, 256);
string strResult = string.Empty;
while (count > 0)
{
strResult += new String(readBuffer,0, count);
count = streamRead.Read(readBuffer,0, 256);
}
arrReturn[2] = strResult;
streamRead.Close();
streamResponse.Close();
myHttpWebResponse.Close();           
return arrReturn;
}

I think the problem is in 我认为问题出在

myRequest.ContentType = "application/x-www-form-urlencoded";

So, from this line you are saying that content is URL-encoded and should be escaped . 因此,从这一行开始,您说的是内容是经过URL编码的,应该进行转义 So I assume, as a quick fix you could change 因此,我认为,作为快速解决方案,您可以进行更改

byte[] buffer = encoding.GetBytes(strPostData);

to

byte[] buffer = encoding.GetBytes(WebUtility.UrlEncode(strPostData));

or something. 或者其他的东西。

But I do believe this intention could be expressed better. 但是我确实相信可以更好地表达这一意图。 So consider using HttpClient and FormUrlEncodedContent: 因此,请考虑使用HttpClient和FormUrlEncodedContent:

    public string[] GetResponseWebAPI(string strPostData, string strUrl)
    {
        string[] arrReturn = new string[3];

        ServicePointManager.ServerCertificateValidationCallback =
            (sender, certificate, chain, sslPolicyErrors) => true;
        var handler = new HttpClientHandler()
        {
            AllowAutoRedirect = false
        };
        using (var client = new HttpClient(handler))
        using (var content =
            new FormUrlEncodedContent(new[] {new KeyValuePair<string, string>("Request", strPostData)}))
        {
            client.Timeout = TimeSpan.FromSeconds(25);
            var response = client.PostAsync(strUrl, content).GetAwaiter().GetResult();

            arrReturn[0] = response.Headers.GetValues("Statuscode").FirstOrDefault();
            arrReturn[1] = response.Headers.GetValues("Statusmessage").FirstOrDefault();
            arrReturn[2] = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
        }

        return arrReturn;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM