Error when creating instance of webservice class

Error is HTTP Error 407 Proxy authentication.

Page 1 of 1

6 Replies - 6314 Views - Last Post: 08 March 2010 - 04:12 AM Rate Topic: -----

#1 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Error when creating instance of webservice class

Posted 05 March 2010 - 09:24 AM

Hi everyone,

I have some code which talks to a web service. It's pretty simple stuff at the moment

string hardwareID = "####-####-####-####-####"; // In code is a ID identifying the hardware of machine
string applicationID = "{random-guid}"; // In code is an identifier guid
byte[] licenseFile = null;

com.example.activateSoftware activate = new com.example.activatesoftware // Error occurs here

if (activate.IsValidLicense("12345-12345-12345-12345-12345"))
   licenseFile = activate.downloadLicenseFile(hardwareID, applicationID, "12345-12345-12345-12345-12345");




The full error was:

"The request failed with HTTP status 407: Proxy Authentication Required (The ISA server requires authorisation to fulfil the request. Access to the Web Proxy service is denied.)"

The proxy service is configured in IE as the user can access the internet through Internet Explorer. The software really needs to be compatible with proxy servers of all kinds. It seems as if ISA is set to use integrated AD authentication.

Please help, I'm tearing my hair out trying to figure this out. I know a way of making a http request with network credentials but how do I apply that to this?

Is This A Good Question/Topic? 0
  • +

Replies To: Error when creating instance of webservice class

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Error when creating instance of webservice class

Posted 05 March 2010 - 09:47 AM

One way to try is to create a WebProxy instance and give it the name of your proxy server and a NetworkCredentials instance. Then set the webservice.Proxy property equal to your web proxy instance. Here's an example (Not tested since I dont have your Web Service) but it should give you an idea

public void ActivateSoftware()
{
    string hardwareID = "####-####-####-####-####"; // In code is a ID identifying the hardware of machine
    string applicationID = "{random-guid}"; // In code is an identifier guid
    byte[] licenseFile = null;

    NetworkCredential cr = new NetworkCredential("username", "password", "domain");
    WebProxy proxy = new WebProxy("http://yourproxy:yourport");

    proxy.Credentials = cr;
    com.example.activateSoftware activate = new com.example.activatesoftware();

    if (activate.IsValidLicense("12345-12345-12345-12345-12345"))
        licenseFile = activate.downloadLicenseFile(hardwareID, applicationID, "12345-12345-12345-12345-12345");
}



Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#3 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: Error when creating instance of webservice class

Posted 05 March 2010 - 11:20 AM

Thanks PyschoCoder,

I was hoping to not have to ask the end user for their proxy server info though. I only wanted to have to ask them a username and password and even then only if it is required. is there a way of retrieving this from internet explorer or using the proxy and authentication that IE uses?

Cheers
Danny
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: Error when creating instance of webservice class

Posted 05 March 2010 - 12:14 PM

http://msdn.microsof...faultproxy.aspx???

EDIT: Nope, that was deprecated. Looks like you want this one.

EDIT 2: Linked to from that page: http://forums.lhotka...9271.aspx#29271
Was This Post Helpful? 1
  • +
  • -

#5 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: Error when creating instance of webservice class

Posted 08 March 2010 - 01:49 AM

OK,

Sorry for the delay in getting back to you.

The bit I'm struggling to understand here is that how does the webservice know to use the proxy?

Is it just an automatic thing. When using a HttpRequest you need to tell the instance of the request to use said proxy. I can't see similar for a webservice.

Thanks for your help
Danny
Was This Post Helpful? 0
  • +
  • -

#6 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: Error when creating instance of webservice class

Posted 08 March 2010 - 02:58 AM

Another update.

I've figured the above out. It automatically use System.Net.WebRequest.DefaultProxy which you can update with appropriate credentials when needed.

Now I need an effective way of telling if a proxy is used and if a proxy requires authentication.

Any ideas. I could try to create a webrequest and if it is successful then there is no need to do anything the defaultproxy is adequate to connect me to the internet (or there isn't a proxy in use) but then if there is a problem get the status code and if it is 407 then ask for credentials.

Another question If I configure the default proxy to use credentials in form load will these credentials continue to be used throughout my program until it exits?

Thanks for the help
Danny
Was This Post Helpful? 0
  • +
  • -

#7 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: Error when creating instance of webservice class

Posted 08 March 2010 - 04:12 AM

I have (managed to come up with a solution for my problem that seems to work.

For anyone that's interested here is my code. It can likely be optimised a bit more but it's working and that's the main thing at the moment
	public void ActivateSoftware()
	{
	    string hardwareID = "####-####-####-####-####"; // In code is a ID identifying the hardware of machine
	    string applicationID = "{random-guid}"; // In code is an identifier guid
	    byte[] licenseFile = null;
   
	    WebClient wc = new WebClient();
	    bool AuthRequired = false;	
		bool successfulConnect = false;
	    
	   	// GetProxy returns the passed address if a proxy is not used, or proxy address if one is used.
		if (WebRequest.DefaultWebProxy.GetProxy(new System.Uri("http://www.example.com/")).ToString() != "http://www.example.com/") {
	   		// A proxy is in use here
	   	
			try {
		    	wc.OpenRead("http://www.example.com/default.aspx");
		    } catch (WebException wx) {
		    	if (((HttpWebResponse)wx.Response).StatusCode == 407)
		    		AuthRequired = true;
	   
		    }
		    
		    if (AuthRequired)
		    {
		    	// Try default credentials (e.g. for ISA with NTLM integration)
		    	WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
		    	AuthRequired=false;	// Assume this will allow us to connect for now
		    	try {
		    		wc.OpenRead("http://www.example.com/default.aspx");	    			
		    	} catch (WebException wx) {
			    	if (((HttpWebResponse)wx.Response).StatusCode == 407)
			    		AuthRequired = true;		    		
		    	}
		    	
		    }
		    
	  	    if (AuthRequired)
	    	{
				//TODO: Request Credentials here
				WebRequest.DefaultWebProxy.Credentials = new NetworkCredential("user", "user");
				AuthRequired=false; // Assume this will allow us to connect for now
				try {
		    		wc.OpenRead("http://www.example.com/default.aspx");	    			
		    	} catch (WebException wx) {
			    	if (((HttpWebResponse)wx.Response).StatusCode == 407)
			    		AuthRequired = true;	
			    	// TODO: Username and password is incorrect. Implement a loop or goto no internet connection screen
		    	}
	  	    }	   	
	  	    
	  	    
	   	} else {
	   		
				try {
		    		wc.OpenRead("http://www.example.com/default.aspx");	    			
		    	} catch (WebException wx) {
			    	if (((HttpWebResponse)wx.Response).StatusCode != 200)
			    		//TODO: No internet connection or my site is down. Goto no internet connection screen
		    	}	   		
	   	}


	    com.example.activateSoftware activate = new com.example.activatesoftware();
	
	    if (activate.IsValidLicense("12345-12345-12345-12345-12345"))
	        licenseFile = activate.downloadLicenseFile(hardwareID, applicationID, "12345-12345-12345-12345-12345");
	
	
	}


It is a bit long winded but I had to account for all the possibilities that clients could have since this will be software that will be sold and used by our clients most of which we have no control over their system setup.

Sorry for the triple post!!

Cheers for the help guys
Danny

This post has been edited by danny_kay1710: 08 March 2010 - 04:13 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1