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"
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..
I have tried with Stream object statement with ..
//string tempString = Encoding.UTF8.GetString(buffer, 0, buffer.Length); &
Read Buffer code change //Int32 count = await 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 . 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:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.