proxy error when consuming web service

J

J Ames

I'm confused on this one. I work behind our company firewall. I created a
web service that accesses an external web site, using the WebProxy class,
and when I test the web service in the browser, it works perfectly. It
passes the correct credentials and returns data as expected. However, when
I attempt to consume the web service in an ASP.NET project, it fails. I'm
getting a http error (bad gateway), so I think it's failing at the proxy.
I'm not sure why it would do that since it sets the proxy credentials in the
service and works on a browser http request.

Is there some other step I need to take?

Thanks,
J
 
C

CaffieneRush

If you're consuming a web service on your local machine then a web
proxy does not need to be set.
 
C

CaffieneRush

My bad, I did not read your question carefully.
So you've already done something like the following.

'Assume RemoteWebService is a remote web service proxy class.
Dim remoteWebService As New RemoteWebService

'WebProxy to get request past company firewall
Dim wp As New WebProxy("http://webproxy.example.com", 80)
'Assign a user credentials to wp
wp.Credentials = New NetworkCredentials("myusername", "mypassword",
"mydomain")

remoteWebService.Proxy = pr

'Call a webmethod on the remote web service
Dim myResults As String = remoteWebService.GetSomeResults()

Andy
 
J

J Ames

What is the reference to "pr"?

Actually, I *didn't* pass anything to the proxy property of the service
object in my client app. I set all that up in the web service itself
directly. So when I do an http test in the browser of the service it uses
the credentials and returns data successfully. It's when I create a client
app to consume that service that the proxy seems to fail. Are you saying I
need to set the proxy property *again* in the client web app?

J
 
J

J Ames

So I need to set the proxy in the web service object in my client app? Not
sure I understand why it's necessary since the service itself is behind the
firewall.
 
J

J Ames

I'm still getting the same error. Would it help to see the service code
itself? I'm pasting it below in case someone can decipher. Thanks.

using System;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Services;
using System.Net;
using System.Web;


/// <summary>
/// Summary description for StockServices.
/// </summary>
[WebService (Namespace="http://enterprise.intranet.com")]
public class StockServices : System.Web.Services.WebService
{
WebProxy myProxy;

public StockServices()
{
InitializeComponent();
}

#region Component Designer generated code

//Required by the Web Services Designer
private IContainer components = null;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing); }

#endregion

[WebMethod]
public string GetQuote(string ticker)
{
string stockURL, page, retval;
try
{
stockURL = GetURL(ticker);
page = GetPageContent(stockURL);
retval = ParsePage(page);
}
catch (ArgumentOutOfRangeException)
{
retval = "Invalid Ticker!";
}
catch (Exception)
{
retval = "Unknown Error";
}

return retval;
}

[WebMethod]
public DataSet GetQuotes(string tickers)
{
char[] splitter = {' '};
string[] _tickers = tickers.Trim().Split(splitter);
Int32 i, ticks;

ticks = _tickers.Length;

DataSet ds = new DataSet();
DataTable dt = new DataTable("StockData");
DataColumn dc;

dc = dt.Columns.Add("Ticker",System.Type.GetType("System.String"));
dc = dt.Columns.Add("Price",System.Type.GetType("System.String"));

for (i = 0; i < ticks; i++)
{
DataRow dr = dt.NewRow();
dr["Ticker"] = _tickers.ToUpper();
dr["Price"] = GetQuote(_tickers);
dt.Rows.Add(dr);
}

ds.Tables.Add(dt);
return ds;
}

public void SetProxy()
{
myProxy = new WebProxy("000.000.000.000", 80);
myProxy.Credentials = new NetworkCredential("sysid", "syspw");
}

private string GetURL(string ticker)
{
StringBuilder url = new StringBuilder();

url.Append("http://finance.yahoo.com/q/ecn?s=");
url.Append(ticker);

return url.ToString();
}

private string ParsePage(string page)
{
Int32 i;

i = page.IndexOf("Last Trade:");
page = page.Substring(i);

i = page.IndexOf("&lt;b>");
page = page.Substring(i);

i = page.IndexOf("&lt;/b>");
page = page.Substring(0,i);

page = Regex.Replace(page,"&lt;b>","");
return page;
}
public string GetPageContent(string url)
{
WebRequest wreq;
WebResponse wres;
StreamReader sr;
String content;

wreq = (HttpWebRequest)WebRequest.Create(url);
SetProxy();
wreq.Proxy = myProxy;
wreq.Credentials = myProxy.Credentials;

// make the HTTP call
wres = (HttpWebResponse)wreq.GetResponse();
sr = new StreamReader(wres.GetResponseStream());
content = sr.ReadToEnd();
sr.Close();

return content;
}
}
 
C

CaffieneRush

Let me get this correct.
Do you have a solution with a web service and another project/web
application that consumes the web service?

If that is the case then you can create a web reference within Visual
Studio in the client project/application to the local web service.
The name of the default web reference to a local web service would be
called localhost (although you call call it whatever you want) and you
can call your local web method directly using this reference.

Thus to call the GetQuote() method in your web service is a simple as:
localhost ws = new localhost();

String quote = ws.GetQuote("GOOG");

When consuming a web service locally there should be no need to use a
WebProxy unless you have a very unusual setup.

Andy

J said:
I'm still getting the same error. Would it help to see the service code
itself? I'm pasting it below in case someone can decipher. Thanks.

using System;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Services;
using System.Net;
using System.Web;


/// <summary>
/// Summary description for StockServices.
/// </summary>
[WebService (Namespace="http://enterprise.intranet.com")]
public class StockServices : System.Web.Services.WebService
{
WebProxy myProxy;

public StockServices()
{
InitializeComponent();
}

#region Component Designer generated code

//Required by the Web Services Designer
private IContainer components = null;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing); }

#endregion

[WebMethod]
public string GetQuote(string ticker)
{
string stockURL, page, retval;
try
{
stockURL = GetURL(ticker);
page = GetPageContent(stockURL);
retval = ParsePage(page);
}
catch (ArgumentOutOfRangeException)
{
retval = "Invalid Ticker!";
}
catch (Exception)
{
retval = "Unknown Error";
}

return retval;
}

[WebMethod]
public DataSet GetQuotes(string tickers)
{
char[] splitter = {' '};
string[] _tickers = tickers.Trim().Split(splitter);
Int32 i, ticks;

ticks = _tickers.Length;

DataSet ds = new DataSet();
DataTable dt = new DataTable("StockData");
DataColumn dc;

dc = dt.Columns.Add("Ticker",System.Type.GetType("System.String"));
dc = dt.Columns.Add("Price",System.Type.GetType("System.String"));

for (i = 0; i < ticks; i++)
{
DataRow dr = dt.NewRow();
dr["Ticker"] = _tickers.ToUpper();
dr["Price"] = GetQuote(_tickers);
dt.Rows.Add(dr);
}

ds.Tables.Add(dt);
return ds;
}

public void SetProxy()
{
myProxy = new WebProxy("000.000.000.000", 80);
myProxy.Credentials = new NetworkCredential("sysid", "syspw");
}

private string GetURL(string ticker)
{
StringBuilder url = new StringBuilder();

url.Append("http://finance.yahoo.com/q/ecn?s=");
url.Append(ticker);

return url.ToString();
}

private string ParsePage(string page)
{
Int32 i;

i = page.IndexOf("Last Trade:");
page = page.Substring(i);

i = page.IndexOf("&lt;b>");
page = page.Substring(i);

i = page.IndexOf("&lt;/b>");
page = page.Substring(0,i);

page = Regex.Replace(page,"&lt;b>","");
return page;
}
public string GetPageContent(string url)
{
WebRequest wreq;
WebResponse wres;
StreamReader sr;
String content;

wreq = (HttpWebRequest)WebRequest.Create(url);
SetProxy();
wreq.Proxy = myProxy;
wreq.Credentials = myProxy.Credentials;

// make the HTTP call
wres = (HttpWebResponse)wreq.GetResponse();
sr = new StreamReader(wres.GetResponseStream());
content = sr.ReadToEnd();
sr.Close();

return content;
}
}




I meant.
pr was an instance of WebProxy used in our production code.

Andy
 
C

CaffieneRush

I see what you're trying to do. You're using a local web service to
scrape the html from an external web site. It looks likes your
webservice should work as written.

To debug, try the following.
Can you get to any breakpoints in the webservice when you call the
webservice from the browser? Yes means local webservice is reachable.
Can you get to any breakpoints in the webservice when you call the
webservice from your web application? Yes means you've created the web
reference to webservice correctly. Can you get past the point in the
code after sending the request to the external website in
GetPageContent()?

Andy
 
J

J Ames

This still doesn't work. The web service is reachable IF you test it
directly in the browser. If I reference it from a local asp.net page that
runs on on a web server on my laptop, it also works fine. If I create a
*new* asp.net project on the same box as the web service and try to consume
it, it returns a 404 error. I've checked the web.config and the URL is
correct. It's not pointing to localhost.
 
C

CaffieneRush

It should be much easier to consume the web service locally than from a
remote web app (laptop).

I suspect you are probably not creating the web reference properly.
The 404 error usually indicates that your request reached the correct
server but IIS could not find the resource requested.

Could you reach any breakpoints in the web service when calling it from
a local web app as I asked about previously?
 
J

J Ames

Yes, I can step thru it in debug mode but as soon as it attempts to call the
very first method it fails with the error.

If there's some other way to create the reference please let me know. I've
tried it in two separate ways:

(1) using the Add Web Reference option, and then typing in the URL of the
WSDL in the field provided. It then allows me to name the reference and add
it to the project...which creates the service.wsdl and the discomap file.

(2) saving the WSDL as a file, using the VS.NET SDK wsdl.exe utility to
manually create a proxy class, then compiling that class into a class
library DLL and then importing that into my project and then creating a
reference to it.

If there's another way please let me know.

Thanks
 
C

CaffieneRush

Those are also the 2 way I know to create the web reference.
If you are already stepping into the web service then the reference was
created and is being called correctly.

So we move on. When you refer to "very first method it fails with the
error" where exactly do you mean?

Is it before or after this call below?
wres = (HttpWebResponse)wreq.GetResponse();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top