How to generate a web request from a Winform Application using a WebService?
In a C# Windows Application, when I try to genrate a web request using the
following code, it is woking fine
Winform code
---------------
Browser.DocumentHTML = "";
string HTMLData="";
string Error = "";
try
{
Uri myUri = new Uri(TxtURL.Text);
CredentialCache credCache = new CredentialCache();
HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(myUri);
MyRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();
StreamReader Reader = new StreamReader(MyResponse.GetResponseStream());
HTMLData = Reader.ReadToEnd();
Reader.Close();
MyResponse.Close();
}
catch(Exception ex)
{
HTMLData = "";
Error = ex.ToString();
}
if (HTMLData != "")
Browser.DocumentHTML = HTMLData;
else
MessageBox.Show(Error);
Here Browser is a AxDHTMLEdit control
But when I tried the following code in a web service it is not working.
Web Service Code
--------------------
[WebMethod]
public string GetURL(string URL, out string Error)
{
string HTMLData="";
Error = "";
try
{
Uri myUri = new Uri(URL);
CredentialCache credCache = new CredentialCache();
HttpWebRequest MyRequest =
(HttpWebRequest)WebRequest.Create(myUri);
MyRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse MyResponse =
(HttpWebResponse)MyRequest.GetResponse();
StreamReader Reader = new
StreamReader(MyResponse.GetResponseStream());
HTMLData = Reader.ReadToEnd();
Reader.Close();
MyResponse.Close();
}
catch(Exception ex)
{
HTMLData = "";
Error = ex.ToString();
}
return HTMLData;
}
Code to Access the Web Service in a Windows Application
------------------------------------------------------------------
TestService MyWebService = new TestService();
string Error = "";
try
{
string HTMLData = MyWebService.GetURL(TxtURL.Text, out Error);
if (HTMLData != "")
Browser.DocumentHTML = HTMLData;
else
{
MessageBox.Show(Error);
}
}
catch(Exception ex)
{
TxtBrowser.Text = ex.ToString();
}
But it generates the exception
System.Net.WebException: The remote server returned an error: (500) Internal
Server Error.
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncRes ult asyncResult)
at System.Net.HttpWebRequest.GetResponse()
at CenService.TunnelService.TunnelURL(String URL, String& Error)
Can anybody please tell me the reason for this error and how to resolve this
issue.
Windows Application Method(Direct)
Windows Application -> Web Site
In this case, it creates the instance of a WebRequest and send to the
WebServer. And get the Response from WebServer and Displays it in a DHTML
edit control.
Web Service Method (Indirect)
Windows Application -> Web Service -> Web Site
In this case, it creates the instance of a WebService from a Windows
Application and through webmethod of that webservice, it sends the url to
WebService.
From WebService, it generates another webrequest to get the web site
(similar to previous method) and get the response inside the webservice and
send the response string to client windows application.
I want to know why it is not allowing to generate a web request from a web
service?
Thanks in advance
Cenray