System.Net.HttpWebRequest.GetResponse()-The remote server returned an error: (500) Internal Server Error



I have the following POST XML code, but no matter how many threads i went through, i never got any satisfactory results to my problem. The following code always shows this error, Remote Server Error 500 in System.Net.HttpWebRequest.GetResponse(), and as far as my knowledge goes, this code is syntactically and logically correct. But i'm failing to understand the error here.


// Part of the another function that passes URI to POST XML Code



string ALL = "MasterInventory";
string URL = "http://ift.tt/1ukRwkD";
Uri uri = new Uri(URL + "?op=" + ALL);


public string TestHTTPPost(Uri url)
{
try
{
string Response = string.Empty;
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(url);
//request.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
MessageBox.Show("URL : " + url.ToString());
request.Method = "POST";
string parameter = "";

XmlDocument Doc = new XmlDocument();
// textBox1.Text contains the XML Request Code
Doc.LoadXml(textBox1.Text);
parameter = Doc.InnerXml.ToString();
parameter = textBox1.Text;
MessageBox.Show("Parameter : " + parameter);

byte[] byteArray = Encoding.UTF8.GetBytes(parameter);
request.ContentLength = byteArray.Length;
request.Credentials = CredentialCache.DefaultCredentials;

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

request.ContentType = "text/xml";

//request.ContentType = "application/json";

//request.ContentType = "application/xml";

request.Method = "POST";
Stream dataStream;
using (dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (HttpStatusCode.OK == response.StatusCode)
{
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
Response = reader.ReadToEnd();
response.Close();
}
return Response;
}

dataStream.Close();
}
}

catch (WebException ex1)
{
return ex1.ToString();
}
}

No comments:

Post a Comment