how to Call web service through HTTP
Hi,
I am writing a middleware web service which will communicate with the host's web service through HTTP GET/POST (not SOAP).
Web App <-----> Middleware Web Service <--------> Host Web Service
When I write a different web application to test the communication to Host Web Service, its working perfectly. (Test Web App <-------> Host Web Service). I use the following method here.
Code: private XmlDocument SendHTTPRequest(string url, XmlDocument xReqDoc)
{
log.Debug("Inside Method");
//Declare XMLResponse document
XmlDocument XMLResponse = null;
//Declare an HTTP-specific implementation of the WebRequest class.
HttpWebRequest objHttpWebRequest;
//Declare an HTTP-specific implementation of the WebResponse class
HttpWebResponse objHttpWebResponse = null;
//Declare a generic view of a sequence of bytes
Stream objRequestStream = null;
Stream objResponseStream = null;
//Declare XMLReader
XmlTextReader objXMLReader;
//Creates an HttpWebRequest for the specified URL.
log.Debug("Creates an HttpWebRequest for the specified URL.");
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
try
{
//---------- Start HttpRequest
//Set HttpWebRequest properties
log.Debug("Set HttpWebRequest properties");
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(xReqDoc.InnerXml);
objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentLength = bytes.Length;
objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
//Get Stream object
log.Debug("Get Stream object ");
objRequestStream = objHttpWebRequest.GetRequestStream();
//Writes a sequence of bytes to the current stream
log.Debug("Writes a sequence of bytes to the current stream");
objRequestStream.Write(bytes, 0, bytes.Length);
//Close stream
log.Debug("Close stream");
objRequestStream.Close();
//---------- End HttpRequest
//Sends the HttpWebRequest, and waits for a response.
log.Debug("Sends the HttpWebRequest, and waits for a response.");
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
//---------- Start HttpResponse
if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
log.Debug("Start HttpResponse");
//Get response stream
objResponseStream = objHttpWebResponse.GetResponseStream();
//Load response stream into XMLReader
log.Debug("Load response stream into XMLReader");
objXMLReader = new XmlTextReader(objResponseStream);
//Declare XMLDocument
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(objXMLReader);
//Set XMLResponse object returned from XMLReader
XMLResponse = xmldoc;
//Close XMLReader
objXMLReader.Close();
}
//Close HttpWebResponse
log.Debug("Close HttpWebResponse");
objHttpWebResponse.Close();
}
catch (WebException we)
{
//TODO: Add custom exception handling
log.Error(we.Message);
}
catch (Exception ex)
{
//throw new Exception(ex.Message);
log.Error(ex.Message);
}
finally
{
//Close connections
if (objRequestStream != null)
objRequestStream.Close();
if (objRequestStream != null)
{
objResponseStream.Close();
}
if (objHttpWebResponse != null)
{
objHttpWebResponse.Close();
}
//Release objects
objXMLReader = null;
objRequestStream = null;
objResponseStream = null;
objHttpWebResponse = null;
objHttpWebRequest = null;
}
//Return
return XMLResponse;
}
But, when I add exactly same method in a class library (DLL) and use it from the actual Middleware Web Service, its giving Internal Server Error (500). The error is thrown after the following line
Code:objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
Both these Test Web App & Middleware Service are hosted in the same IIS. Could anyone give a hand to solve this?
Thanks