473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

HttpWebRequest Changes from .NET 1.1 to 2.0: Proxy Authentication

Hi

I have a utility class, called MailHandler, that I wrote to read and operate
on emails on an Exchange server using WebDAV. The WebDAV SQL statements are
sent using an HttpWebRequest object.

This worked fine in a .NET 1.1 project. I have copied the same Mailhandler
class into a .NET 2.0 project. This, too, worked fine when I initially
tested it, running as a Windows Forms app on my machine (running under my
network account). However, when I installed the app running as a Windows
service under the local system account, I got a Proxy Authentication Required
error (status 407). The .NET 1.1 project was also running as a Windows
service on the same machine, running under the same account. It was also
connecting to the same mailbox on the same Exchange server.

I've run an Ethereal trace on the outputs from the two projects. The .NET
1.1 project is communicating directly with the Exchange server. The .NET 2.0
project is communicating with the proxy server rather than the Exchange
server.

Obviously there has been some change in the HttpWebRequest class between
..NET 1.1 and .NET 2.0 that would cause the .NET 2.0 version to use the proxy
server where the .NET 1.1 version doesn't. Does anyone know what has
changed, and how I could bypass the proxy server in .NET 2.0?

Here is the relevant code from the MailHandler class. It returns an XML
document containing a list of all the emails in a specified folder in a
specified mailbox (I've left out everything after the HttpWebResponse is
returned):

public XmlDocument GetFolderEmails(string sFolder)
{
// Where _mailboxURI = "http://" + sMailServer + "/exchange/" + sMailbox.
// sMailServer is of the form <ExchangeServerName>@<CompanyName>.co.nz.
// sMailbox is the name of the mailbox, eg InwardsJobs.
// sFolder is the name of a mail folder within the mailbox, eg Inbox.
string sFolderURI = _mailboxURI;
string sQuery;
string sErrMsg;
HttpWebRequest Request;
HttpWebResponse Response = null;
byte[] bytes = null;
Stream RequestStream = null;
Stream ResponseStream = null;
int iStatusCode = 0;
XmlDocument ResponseXMLDoc = null;

try
{
sFolder = sFolder.Replace(" ", "%20");
if (!sFolderURI.EndsWith("/"))
{
sFolderURI += "/";
}
sFolderURI += sFolder;

sQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
+ "<D:sql>SELECT "
+ "\"urn:schemas:mailheader:message-id\", "
+ "\"urn:schemas:httpmail:subject\", "
+ "\"urn:schemas:httpmail:fromemail\", "
+ "\"urn:schemas:httpmail:fromname\", "
+ "\"urn:schemas:httpmail:date\", "
+ "\"urn:schemas:httpmail:datereceived\", "
+ "\"urn:schemas:httpmail:textdescription\" "
+ "FROM \"" + sFolderURI + "\" "
+ "WHERE \"DAV:ishidden\" = false "
+ "AND \"DAV:isfolder\" = false"
+ "</D:sql></D:searchrequest>";

Request = (HttpWebRequest)HttpWebRequest.Create(sFolderURI);
Request.Credentials = _mailCredentials;
Request.Method = "SEARCH";

// Write the SQL query to the request stream.
bytes = Encoding.UTF8.GetBytes(sQuery);
Request.ContentLength = bytes.Length;
RequestStream = Request.GetRequestStream();
RequestStream.Write(bytes, 0, bytes.Length);
RequestStream.Close();

Request.ContentType = "text/xml";

Response = (HttpWebResponse)Request.GetResponse();
iStatusCode = (int)Response.StatusCode;

<More code here to deal with returned XML document>

catch (Exception xcp)
{
<Exception handling code here>
}
finally
{
if (Response != null)
{
Response.Close();
}
}

return ResponseXMLDoc;
}

_mailCredentials is a CredentialCache object that is set as follows:
_mailCredentials = new CredentialCache();
_mailCredentials.Add( new System.Uri(_mailboxURI),
"NTLM",
new System.Net.NetworkCredential(sUser, sPwd, sDomain)
);
where sUser, sPwd and sDomain are username, password and domain required to
log into the mailbox, and _mailboxURI is the same as in the code above.

Cheers
Si
Mar 6 '07 #1
2 5657
"SimonDev" <Si******@discussions.microsoft.comwrote in message
news:FF**********************************@microsof t.com...
Hi
....
This worked fine in a .NET 1.1 project. I have copied the same
Mailhandler
class into a .NET 2.0 project. This, too, worked fine when I initially
tested it, running as a Windows Forms app on my machine (running under my
network account). However, when I installed the app running as a Windows
service under the local system account, I got a Proxy Authentication
Required
error (status 407). The .NET 1.1 project was also running as a Windows
service on the same machine, running under the same account. It was also
connecting to the same mailbox on the same Exchange server.
....
Obviously there has been some change in the HttpWebRequest class between
.NET 1.1 and .NET 2.0 that would cause the .NET 2.0 version to use the
proxy
server where the .NET 1.1 version doesn't.
This could also be a change in a configuration file, perhaps machine.config.

John
Mar 7 '07 #2
Thanks a lot, John!

I compared the two machine.config files and found that the .NET 1.0 one had
a system.net section with a <defaultProxyelement whereas the .NET 2.0 file
did not. I copied that <defaultProxyelement to my .NET 2.0 app.config file
and that seemed to solve the problem.

Cheers
Si
"John Saunders" wrote:
>
This could also be a change in a configuration file, perhaps machine.config.

John
Mar 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ME | last post by:
Hi; I am getting "Unhandled Exception: System.Net.WebException: The remote server returned an erro r: (401) Unauthorized." when I am trying to get a page via post. Code follows...
15
by: warlord | last post by:
I have a windows client app that is trying to download a file from a web server but I always get the following error when I call the GetResponse method of the Request object. The remote server...
0
by: Pmcg | last post by:
I would appreciate any help with the following, havn't found any answers for this in my research to date. I am trying to retreive a web page (a htm file intially) from a vdir on an intranet site...
8
by: Dave Brown | last post by:
I am attempting to post to a url (https://FakeURL/logon.asp) using the HttpWebRequest class. The response for a succesful post will contain the html for the logon user's default page. We've...
1
by: cowbell | last post by:
Providing authentication for protected content via Basic/Digest Headers seems pretty straight-forward, but I'm stuck trying to figure out how to do something a little different: say you have...
4
by: retroviz | last post by:
Hi there. I have written a screen scraping application (both web based and windows forms) in vb.net. When testing on a public broadband link it works fine. However it fails at work due to our...
2
by: =?Utf-8?B?TGFycnlLdXBlcm1hbg==?= | last post by:
Our WebDev team seems to have found a problem that exposes a bug in .NET 2.0. This problem can be shown when trying to access a WebService using SSL and through a proxy server after using the...
2
by: =?Utf-8?B?TGVuc3Rlcg==?= | last post by:
A C# (.NET 2) application which uses the System.Net.HttpWebRequest object to request a resource over HTTPS is failing following the installation of a new proxy server on our internal network with...
1
by: Proogeren | last post by:
I have a problem with a httpwebrequest that I am creating. The request in itself looks correct but using fiddler I see that a www-authentication header is sent along as well. The code is pasted...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.