Click here to Skip to main content
15,867,704 members
Articles / Programming Languages / Visual Basic
Article

Connect to a web service through a Proxy Server

Rate me:
Please Sign up or sign in to vote.
4.88/5 (26 votes)
29 Jun 20032 min read 724.4K   71   45
Connect to a web service through a Proxy Server

Introduction

This article explains the how you access a webservice through a Proxy Server.

Using the code

In order to show an example of this, we will use a public web service provided by Google. To use this ws, we need to obtain a Validation Key from Google. Next we defined the search criteria and we display the total of obtained registries.

We use the following libraries :

NetworkCredential Class

Provides credentials for password-based authentication schemes such as basic, digest, NTLM, and Kerberos authentication. For a list of all members of this type, see NetworkCredential Members. The NetworkCredential class is a base class that supplies credentials in password-based authentication schemes such as basic, digest, NTLM, and Kerberos. Classes that implement the ICredentials interface, such as the CredentialCache class, return NetworkCredential instances. This class does not support public key-based authentication methods such as SSL client authentication.

WebProxy Class

Contains HTTP proxy settings for the WebRequest class. For a list of all members of this type, see WebProxy Members. The WebProxy class contains the proxy settings that WebRequest instances use to override the proxy settings in GlobalProxySelection. The WebProxy class is the base implementation of the IWebProxyinterface.

The Proxy Server IP Adress is 127,0,1,2 and the port is the 80. 
The data of the user for the authentication with the proxy are: 
User Id: user 
Password: pwd 
Domain: MyDomain 

The following VB.NET code shows an example:

VB.NET
' Search button: do a search, display number of results 
Private Sub btnSearch_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles btnSearch.Click 

' Create a Google Search object 
Dim s As New Google.GoogleSearchService 

Try 

' google params 
Dim strLicenceseKey As String = "google license key" ' google license key 
Dim strSearchTerm As String = "Bruno Capuano" ' google license key 

' proxy settings 
Dim cr As New System.Net.NetworkCredential("user", "pwd", "MyDomain") 
Dim pr As New System.Net.WebProxy("127.0.1.2", 80) 

pr.Credentials = cr 
s.Proxy = pr 

' google search
Dim r As Google.GoogleSearchResult = s.doGoogleSearch(strLicenceseKey, _
  strSearchTerm, 0, 10, True, "", False, "", "", "")
' Extract the estimated number of results for the search and display it
Dim estResults As Integer = r.estimatedTotalResultsCount 

MsgBox(CStr(estResults))

Catch ex As System.Web.Services.Protocols.SoapException

MsgBox(ex.Message)

End Try

End Sub 

If you want to use the current user credential you can use the DefaultCredentials Property. The DefaultCredentials property applies only to NTLM, negotiate, and Kerberos-based authentication. Sample :

VB.NET
' set default credentials
pr.Credentials = System.Net.CredentialCache.DefaultCredentials 

DefaultCredentials represents the system credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. For ASP.NET applications, the default credentials are the user credentials of the logged-in user, or the user being impersonated.

Note - The ICredentials instance returned by DefaultCredentials cannot be used to view the user name, password, or domain of the current security context.

Conclusion

This is my first article and a very simple one. Any questions or suggestions are welcome ! Bye !

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Now I'm Spain. 100 % Vs.Net development working today, ! .. and trying to reload !!

Comments and Discussions

 
GeneralRe: GetDefaultProxy? Pin
LUAL26-Oct-10 3:54
LUAL26-Oct-10 3:54 
Generalkerberos Pin
zerdust_reso22-Sep-03 21:24
zerdust_reso22-Sep-03 21:24 
QuestionThe request failed with HTTP status 407: Proxy Access Denied? Pin
Nguyen MInh Tuan21-Jul-03 17:09
Nguyen MInh Tuan21-Jul-03 17:09 
AnswerRe: The request failed with HTTP status 407: Proxy Access Denied? Pin
Bruno Capuano25-Jul-03 11:43
Bruno Capuano25-Jul-03 11:43 
GeneralRe: The request failed with HTTP status 407: Proxy Access Denied? Pin
Anonymous29-Jul-03 16:04
Anonymous29-Jul-03 16:04 
QuestionHow to use the default credentials Pin
Roger Willcocks1-Jul-03 10:47
Roger Willcocks1-Jul-03 10:47 
AnswerRe: How to use the default credentials Pin
Bruno Capuano2-Jul-03 3:11
Bruno Capuano2-Jul-03 3:11 
GeneralRe: How to use the default credentials Pin
Roger Willcocks2-Jul-03 12:21
Roger Willcocks2-Jul-03 12:21 
This works if the ISA set to Integrated AuthType, but not if Basic.


PS Web Applications and Windows applications are quite different because Windows app don’t have a IIS behind and you can use impersonation in aweb application.

GeneralRe: How to use the default credentials Pin
Anonymous22-Jan-04 14:08
Anonymous22-Jan-04 14:08 
AnswerRe: How to use the default credentials Pin
Jigs Shah30-Jun-08 18:52
Jigs Shah30-Jun-08 18:52 
GeneralA Wrench... SSL Pin
Anonymous1-Jul-03 6:05
Anonymous1-Jul-03 6:05 
GeneralRe: A Wrench... SSL Pin
Bruno Capuano1-Jul-03 7:04
Bruno Capuano1-Jul-03 7:04 
GeneralRe: A Wrench... SSL Pin
Mike McPhail13-Aug-03 10:36
Mike McPhail13-Aug-03 10:36 
GeneralOh! Baby.... Pin
Anonymous30-Jun-03 10:56
Anonymous30-Jun-03 10:56 
GeneralRe: Oh! Baby.... Pin
Bruno Capuano30-Jun-03 11:00
Bruno Capuano30-Jun-03 11:00 
GeneralRe: Oh! Baby.... Pin
Leonard Guilmerd30-Jun-03 16:28
Leonard Guilmerd30-Jun-03 16:28 
GeneralT'Mocha Pin
Anonymous30-Jun-03 9:58
Anonymous30-Jun-03 9:58 
GeneralRe: T'Mocha Pin
Bruno Capuano30-Jun-03 10:31
Bruno Capuano30-Jun-03 10:31 
GeneralOpinion Pin
Anonymous30-Jun-03 7:50
Anonymous30-Jun-03 7:50 
GeneralRe: Opinion Pin
Bruno Capuano30-Jun-03 7:51
Bruno Capuano30-Jun-03 7:51 
Generalvos sos Pin
jplotero30-Jun-03 7:43
jplotero30-Jun-03 7:43 
GeneralRe: vos sos Pin
Bruno Capuano30-Jun-03 7:45
Bruno Capuano30-Jun-03 7:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.