468,247 Members | 1,468 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

Generating a web request from a Winform Application using WebServi

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

Jul 21 '05 #1
0 2562

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by localhost | last post: by
5 posts views Thread by TomR | last post: by
4 posts views Thread by Lee Chapman | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.