473,398 Members | 2,088 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,398 software developers and data experts.

HttpWebRequest problem with WWW-authenticate header

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 below. I do not add any www-authentication header here so I was wondering if anyone knows how to remove it.
I have used almost 2 days trying to figure this out so help would be highly appreciated.

CORRECT
No proxy-authenticate header is present
no www-authenticate header is present

WRONG
No proxy-authenticate header is present
www-authenticate header is present. Basic realm="Portal Agent"

So my problem is to remove the www-authenticate header

Expand|Select|Wrap|Line Numbers
  1. public void WebRequestUploadTes ()
  2.         {
  3.             string url = "http://test/foto/bin/upload.dll";
  4.             string file = @"C:\Untitled.jpg";
  5.             UploadFilesToRemoteUrl(url, file);
  6.         }
  7.  
  8.         private void UploadFilesToRemoteUrl(string url, string file)
  9.         {
  10.             string boundary = "--" + DateTime.Now.Ticks.ToString("x");
  11.  
  12.             // Create the web request
  13.             HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  14.             httpWebRequest.Method = "POST";
  15.             httpWebRequest.ServicePoint.Expect100Continue = false;
  16.  
  17.             byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
  18.  
  19.             string header = "Content-Disposition: form-data; name=\"username\" \r\n\r\ntesuser ";
  20.             header += "\r\n--" + boundary + "\r\n";
  21.             header += "Content-Disposition: form-data; name=\"password\" \r\n\r\n123 ";
  22.             header += "\r\n--" + boundary + "\r\n";
  23.  
  24.             // file upload
  25.             header += "Content-disposition: attachment; name=\"file1\"; filename=\"Oryx Antelope.jpg\"\r\n";
  26.             header += "Content-type: application/octet-stream\r\n";
  27.             header += "Content-Transfer-Encoding: binary\r\n";
  28.  
  29.  
  30.             //convert the header to a byte array
  31.             byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
  32.  
  33.             // Add all of the content up.
  34.             httpWebRequest.ContentLength = new FileInfo(file).Length + headerbytes.Length + (boundarybytes.Length * 2) + 2;
  35.  
  36.             httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
  37.  
  38.             httpWebRequest.Headers["Pragma"] = "no-cache";
  39.  
  40.             // Get the output stream
  41.             Stream requestStream = httpWebRequest.GetRequestStream();
  42.  
  43.             // Write out the starting boundry
  44.             requestStream.Write(boundarybytes, 0, boundarybytes.Length);
  45.  
  46.             // Write the header including the filename.
  47.             requestStream.Write(headerbytes, 0, headerbytes.Length);
  48.             // Open up a filestream.
  49.             FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
  50.  
  51.             // Use 4096 for the buffer
  52.             byte[] buffer = new byte[4096];
  53.  
  54.             int bytesRead = 0;
  55.             // Loop through whole file uploading parts in a stream.
  56.             while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  57.             {
  58.                 requestStream.Write(buffer, 0, bytesRead);
  59.                 requestStream.Flush();
  60.             }
  61.  
  62.             boundarybytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
  63.  
  64.             // Write out the trailing boundry
  65.             requestStream.Write(boundarybytes, 0, boundarybytes.Length);
  66.  
  67.             // Close the request and file stream
  68.             requestStream.Close();
  69.             fileStream.Close();
  70.  
  71.  
  72.             try
  73.             {
  74.                 WebResponse webResponse = httpWebRequest.GetResponse();
  75.  
  76.                 Stream responseStream = webResponse.GetResponseStream();
  77.                 StreamReader responseReader = new StreamReader(responseStream);
  78.  
  79.                 string responseString = responseReader.ReadToEnd();
  80.  
  81.                 // Close response object.
  82.                 webResponse.Close();
  83.  
  84.             }
  85.             catch (Exception)
  86.             { }
  87.         }
  88.  
  89.  
  90.  
  91.  
Jul 20 '08 #1
1 7967
Plater
7,872 Expert 4TB
It would get added in if the server returned a status requiring it (401 unauthorized)
Did you watch the entire series of transactions? I bet somewhere the server made that request?
Jul 21 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kueishiong Tu | last post by:
I have a url, I pass it to Webclient, and I get response without any problem. String* uriString = S"trade7.masterlink.com.tw/futures/QuotePrice.jsp"; String* postData = S""; // Create a new...
9
by: Mike Cronin via DotNetMonster.com | last post by:
Hi there, Can anyone tell me what level of encryption is used when making an HTTPS POST request through an instance of the System.Net.HttpWebRequest object? Thanks much in advance! Mike...
1
by: etantonio | last post by:
Good Morning, I need to read a web page, to do this I use the following code that works well if I choose sAddressTime = "http://www.etantonio.it/it/index.aspx" and you can see the trace...
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...
16
by: thomas peter | last post by:
I am building a precache engine... one that request over 100 pages on an remote server to cache them remotely... can i use the HttpWebRequest and WebResponse classes for this? or must i use the...
4
by: R Reyes | last post by:
I am trying to code a file uploader (for forum/email attachments) from the client computer to a remote web server via the PUT method (since POST is not allowed ). However, the upload works ONLY...
1
by: Jeff B | last post by:
I'm trying to create a simple screen scraping application and I kept getting a System.Net.WebException thrown back with a message of "The operation has timed-out." At first I thought it was some...
11
by: Keith Patrick | last post by:
Could someone explain to me the relationship between these two classes? I am ripping my hair out trying to divert an HttpRequest to a new location via an HttpWebRequest, but I cannot get my...
1
by: sfoxover | last post by:
Hi, Could someone please give me some suggestions on how to make this class robust. I need to be able to handle around 20 similtanious requests to this class which causes a web browser to...
12
by: Mark Rae | last post by:
Hi, The following code works: HttpWebRequest objRequest = null; try { HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com"); using (HttpWebResponse...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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,...
0
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...

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.