By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,360 Members | 2,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,360 IT Pros & Developers. It's quick & easy.

HttpWebRequest problem with WWW-authenticate header

P: 36
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
Share this Question
Share on Google+
1 Reply


Plater
Expert 5K+
P: 7,872
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

Post your reply

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