Click here to Skip to main content
11,431,844 members (63,277 online)
Click here to Skip to main content

Connect to a web service through a Proxy Server

, 29 Jun 2003
Rate this:
Please Sign up or sign in to vote.
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:

' 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 :

' 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

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 5 Pin
alain_dionne2-Mar-12 10:43
memberalain_dionne2-Mar-12 10:43 
GeneralDowload file from internet through proxy in vb.net Pin
vu van phuong1-Jan-09 22:22
membervu van phuong1-Jan-09 22:22 
QuestionAdding proxyserver to authenticate Pin
v-11dals26-Nov-08 19:10
memberv-11dals26-Nov-08 19:10 
GeneralError consuming WS. Pin
VladValle25-Jun-08 6:22
memberVladValle25-Jun-08 6:22 
GeneralCreate proxy server using c# Pin
Arron Xiao13-Aug-07 22:58
memberArron Xiao13-Aug-07 22:58 
QuestionCCproxy Problem Pin
bkashok28-Feb-07 1:03
memberbkashok28-Feb-07 1:03 
i am working in donet 1.1 windows application. I am accessing the webservices using proxy settings. If i use winproxy means its working fine.. But if i use CCProxy.. the following code will not work for sometimes.

System.Net.WebProxy WProxy = new System.Net.WebProxy((
ProxyHost,ProxyPort);
if((ProxyLogin.Trim().Length > 0) &&(ProxyPassword.Trim().Length > 0))
{
System.Net.NetworkCredential cred = new System.Net.NetworkCredential(ProxyLogin,ProxyPassword,ProxyDomain);
WProxy.Credentials = cred;
}

Its giving the error:
The request failed with HTTP status 407 : Unauthorized.

Please give me the solution for this..


Best regards
Christy
GeneralQuestion about proxy in webrequest (!!Help!!) Pin
stancrm3-May-06 3:19
memberstancrm3-May-06 3:19 
QuestionProxy with authentication retire something. Pin
icerein4-Dec-05 23:52
membericerein4-Dec-05 23:52 
GeneralTrying to create a app through proxies Pin
Anonymous28-May-05 19:06
sussAnonymous28-May-05 19:06 
GeneralRe: Trying to create a app through proxies Pin
Bruno Capuano29-May-05 21:20
memberBruno Capuano29-May-05 21:20 
Generalcomunicación con formularios Pin
Carlos Cruz Espino24-Jan-05 15:17
sussCarlos Cruz Espino24-Jan-05 15:17 
GeneralRe: comunicación con formularios Pin
Bruno Capuano25-Jan-05 3:09
memberBruno Capuano25-Jan-05 3:09 
GeneralThe request failed with HTTP status 417: Expectation Failed. Pin
dbrizuela23-Nov-04 9:17
memberdbrizuela23-Nov-04 9:17 
GeneralRe: The request failed with HTTP status 417: Expectation Failed. Pin
Bruno Capuano25-Jan-05 3:15
memberBruno Capuano25-Jan-05 3:15 
GeneralRe: The request failed with HTTP status 417: Expectation Failed. Pin
dbrizuela25-Jan-05 3:55
memberdbrizuela25-Jan-05 3:55 
GeneralRe: The request failed with HTTP status 417: Expectation Failed. Pin
Bruno Capuano25-Jan-05 5:54
memberBruno Capuano25-Jan-05 5:54 
GeneralProxy with authentication (SQUID) Pin
Alberto Gastaldo25-Aug-04 2:34
sussAlberto Gastaldo25-Aug-04 2:34 
GeneralRe: Proxy with authentication (SQUID) Pin
Bruno Capuano13-Sep-04 11:33
memberBruno Capuano13-Sep-04 11:33 
GeneralRe: Proxy with authentication (SQUID) Pin
MimmoEv30-Nov-05 6:30
memberMimmoEv30-Nov-05 6:30 
GeneralRe: Proxy with authentication (SQUID) [modified] Pin
Michael Freidgeim6-Sep-06 21:29
memberMichael Freidgeim6-Sep-06 21:29 
GeneralProxy with authentication (SQIOD) Pin
Alberto Gastaldo25-Aug-04 2:33
sussAlberto Gastaldo25-Aug-04 2:33 
QuestionGetDefaultProxy? Pin
Tom Gaskins5-Jun-04 15:57
memberTom Gaskins5-Jun-04 15:57 
AnswerRe: GetDefaultProxy? Pin
Bruno Capuano7-Jun-04 6:18
memberBruno Capuano7-Jun-04 6:18 
GeneralRe: GetDefaultProxy? Pin
LUAL26-Oct-10 4:54
memberLUAL26-Oct-10 4:54 
Generalkerberos Pin
reso88@hotmail.com22-Sep-03 22:24
memberreso88@hotmail.com22-Sep-03 22:24 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 30 Jun 2003
Article Copyright 2003 by Bruno Capuano
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid