473,490 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HTTPs using httpwebrequest object

Hi there,

I am very stuck and have tried everything on this one!

I am trying to get to an https website and cannot work out how to
logon. When I log on manually, I have to enter username and password in
a dialog box.

I am trying to use Basic Authorisation to get in and everything I try
returns a 401 unauthoriased error. I am sure this should be simple :-(

Thanks,

Phil

HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("https://www.website.co.uk");
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "GET";

string base64EncodedAuthorizationString = "user" + ":" + "pass";
byte[] binaryData = new Byte[base64EncodedAuthorizationString.Length];
binaryData = Encoding.ASCII.GetBytes(base64EncodedAuthorization String);
base64EncodedAuthorizationString =
Convert.ToBase64String(binaryData);
base64EncodedAuthorizationString = "Basic " +
base64EncodedAuthorizationString;
req.Headers.Add("Authorization", base64EncodedAuthorizationString);

HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();

Jan 10 '07 #1
1 2097
Thus wrote ph*********@starlizard.com,
Hi there,

I am very stuck and have tried everything on this one!

I am trying to get to an https website and cannot work out how to
logon. When I log on manually, I have to enter username and password
in a dialog box.

I am trying to use Basic Authorisation to get in and everything I try
returns a 401 unauthoriased error. I am sure this should be simple :-(

Thanks,

Phil

HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("https://www.website.co.uk");
req.ContentType = "application/x-www-form-urlencoded"; req.Method =
"GET";

string base64EncodedAuthorizationString = "user" + ":" + "pass";
byte[] binaryData = new Byte[base64EncodedAuthorizationString.Length];
binaryData =
Encoding.ASCII.GetBytes(base64EncodedAuthorization String);
base64EncodedAuthorizationString =
Convert.ToBase64String(binaryData);
base64EncodedAuthorizationString = "Basic " +
base64EncodedAuthorizationString;
req.Headers.Add("Authorization", base64EncodedAuthorizationString);
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
The BCL already contains the required functionality for HTTP authentication.
Simply create a NetworkCredential instance and assign it to HttpWebRequest.Credentials.
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("https://www.website.co.uk");
request.Credentials = new NetworkCredential("foo", "bar");
using (HttpWebResponse response = (HttpWebResponse) req.GetResponse()) {
// process response
}

You can also put a whole set of credentials in a CredentialCache, and use
that instead of NetworkCredential. The system wil figure which credential
is required for a particular web site.
Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Jan 10 '07 #2

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

Similar topics

2
20078
by: dgiard | last post by:
I am using the System.Net.HttpWebRequest object to POST data to an HTTPS web page. Other than prefixing the URL with "HTTPS://", do I need to do anything in my code to indicate that this is SSL?...
0
1877
by: Subra Mallampalli | last post by:
Hi, I am trying to connect over HTTPS using a client certificate. I am able to connect fine when using IE. From my application, however, I get the following error: The underlying connection...
2
2254
by: Jonathan Wax | last post by:
Hi, I spent the last week looking for an answer to the following question: How can you upload an xml file to an HTTPS server with a specific certificate. Basically doing the same as this html...
0
325
by: phil.jacobs | last post by:
Hi there, I am very stuck and have tried everything on this one! I am trying to get to an https website and cannot work out how to logon. When I log on manually, I have to enter username and...
6
7799
by: nganapat | last post by:
I am trying to post form values to a https web page programmatically using Httpwebrequest but no matter what I do the same login page is returned instead of the next page. I would very much...
2
5395
by: =?Utf-8?B?RGF2aWQgS2lya2xhbmQ=?= | last post by:
Hi, I am trying to connect to an https server and download a file. The server requires a client side certificate, which I have loaded successfully using the X509Certificate class. I then add this...
0
4021
by: shlim | last post by:
Currently I'm using VB.Net to perform a http/https multipart form post to a servlet. I'm able to perform the post using HttpWebrequest via GetRequestStream(). However, the servlet returned me with...
2
8109
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...
3
1908
by: wzb6 | last post by:
Hi, I am trying to post form values to a https web page programmatically using Httpwebrequest but no matter what I do the same login page is returned instead of the next page. I would very much...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
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,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.