Here is a sample Windows Application that retrieve an icon on an remote
(Internet) web server.
--------------------------------------------------
Dim HttpWReq As HttpWebRequest = CType(WebRequest.Create
("http://sg.geocities.com/mangokun/images/settings.ico"), HttpWebRequest)
' Turn off connection keep-alives.
HttpWReq.KeepAlive = False
' Use only if needed
'HttpWReq.Proxy = New WebProxy("http://proxyname:80/")
Dim HttpWResp As HttpWebResponse = CType(HttpWReq.GetResponse(),
HttpWebResponse)
Dim imgstream As System.IO.Stream= HttpWResp.GetResponseStream()
Dim img As Image= Image.FromStream(imgstream)
HttpWResp.Close()
TextBox1.Text = HttpWResp.ResponseUri.ToString
txtimgName.Text = Path.GetFileName(HttpWResp.ResponseUri.ToString)
txtimgDimension.Text = img.PhysicalDimension.ToString
PictureBox1.Image = img
--------------------------------------------------
If you might have noticed, I have commented out the line: HttpWReq.Proxy =
New WebProxy("http://proxyname:80/"), because the proxy is either not
needed, or it auto-detected the proxy.
For your GetWeather Web Method, the only thing missing is that you need
this line above objWRQ.GetResponse():
objWRQ.Proxy = New WebProxy("http://yourproxynamehere:80/")
' -- 3. Get the response back from the remote server
objWRS = objWRQ.GetResponse() ----> error occurs here
-----
Let me explain a bit on proxy:
In a Windows Application, proxy will be auto-detected when you call a
remote web service method (outside your Intranet).
But for Web Applications and Web Services, this may not be the case.
Therefore you have to explicitly set the proxy setting for the accessing
object.
> Hi,
>
> We tried with CityWeather Webservice example given in Introducing .NET by
> Wrox Press. We have created CityWeather and TestCityWeather applications
in
> wwwroot. While running Webservice it gives the error:
>
> System.Net.WebException: The system was unable to submit the request: The
> remote name could not be resolved.
> at System.Net.HttpWebRequest.CheckFinalStatus()
> at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult AsyncResult)
> at System.Net.HttpWebRequest.GetResponse()
> at CityWeather.CityWeather.GetWeather(String strZipCode) in
> c:\inetpub\wwwroot\CityWeather\CityWeather.vb:line 59
>
> Machine which has Webservice is connected to Internet through Proxy. But
if
> intranet address (our intranet web) is given for URI, it works fine. We
> tested the URL "http://weather.searchwho.com/cgi-bin/myweather.pl" in the
> Browser directly and it returns the weather details. Suggestions and
> Solutions would be appreciated.
>
>
> GetWeather Web Method:
>
> Public Function <WebMethod()> GetWeather(ByVal strZipCode As String)
_
> As
String
> ' -- Declare variables
> Dim strResult As String ' Our result
> Dim strHTML As String ' The HTML we expect back
> Dim strURI As String ' The URL/URI we are going to hit
> Dim objWRQ As System.Net.WebRequest ' The Request object
> Dim objWRS As System.Net.WebResponse ' The Response object
> Dim objWRF As System.Net.WebRequestFactory ' The Request object
> creator
> Dim objStream As System.IO.StreamReader ' The stream of data
coming
> back
>
> ' -- SearchWho.com URL to access
> Const WEATHER_URI As String
> "http://weather.searchwho.com/cgi-bin/myweather.pl"
> ' -- 1. Build our URL using the Zip code sent in
> strURI = WEATHER_URI & "?loc=" & strZipCode
> ' -- 2. Create a Request object based on our URI,
> 'and call the remote server
> objWRQ = objWRF.Create(strURI)
> ' -- 3. Get the response back from the remote server
> objWRS = objWRQ.GetResponse() ----> error occurs here
> ' -- 4. Read the response into a String format
> objStream = New System.IO.StreamReader(objWrs.GetResponseStream
(), _
>
> System.Text.Encoding.ASCII)
>
> ....................
> ....................
> ....................
>
> End Function
>
> Regards,
> - Vijay
>
>