473,396 Members | 1,783 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,396 software developers and data experts.

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 2094
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
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
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
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
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
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
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
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
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
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
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
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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...

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.