none
HttpWebRequest problem with WWW-authenticate header

    Question

  •  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

    public void WebRequestUploadTes ()  
            {  
                string url = "http://test/foto/bin/upload.dll";  
                string file = @"C:\Untitled.jpg";  
                UploadFilesToRemoteUrl(url, file);  
            }  
       
            private void UploadFilesToRemoteUrl(string url, string file)  
            {  
                string boundary = "--" + DateTime.Now.Ticks.ToString("x");  
       
                // Create the web request  
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);  
                httpWebRequest.Method = "POST";  
                httpWebRequest.ServicePoint.Expect100Continue = false;  
       
                byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n");  
                         
                string header = "Content-Disposition: form-data; name=\"username\" \r\n\r\ntesuser ";  
                header += "\r\n--" + boundary + "\r\n";  
                header += "Content-Disposition: form-data; name=\"password\" \r\n\r\n123 ";  
                header += "\r\n--" + boundary + "\r\n";  
       
                // file upload  
                header += "Content-disposition: attachment; name=\"file1\"; filename=\"Oryx Antelope.jpg\"\r\n";  
                header += "Content-type: application/octet-stream\r\n";  
                header += "Content-Transfer-Encoding: binary\r\n";  
       
       
                //convert the header to a byte array  
                byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);  
       
                // Add all of the content up.  
                httpWebRequest.ContentLength = new FileInfo(file).Length + headerbytes.Length + (boundarybytes.Length * 2) + 2;  
       
                httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;  
       
                httpWebRequest.Headers["Pragma"] = "no-cache";  
       
                // Get the output stream  
                Stream requestStream = httpWebRequest.GetRequestStream();  
       
                // Write out the starting boundry  
                requestStream.Write(boundarybytes, 0, boundarybytes.Length);  
       
                // Write the header including the filename.  
                requestStream.Write(headerbytes, 0, headerbytes.Length);  
                // Open up a filestream.  
                FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);  
       
                // Use 4096 for the buffer  
                byte[] buffer = new byte[4096];  
       
                int bytesRead = 0;  
                // Loop through whole file uploading parts in a stream.  
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)  
                {  
                    requestStream.Write(buffer, 0, bytesRead);  
                    requestStream.Flush();  
                }  
       
                boundarybytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");  
       
                // Write out the trailing boundry  
                requestStream.Write(boundarybytes, 0, boundarybytes.Length);  
       
                // Close the request and file stream  
                requestStream.Close();  
                fileStream.Close();  
       
       
                try  
                {  
                    WebResponse webResponse = httpWebRequest.GetResponse();  
       
                    Stream responseStream = webResponse.GetResponseStream();  
                    StreamReader responseReader = new StreamReader(responseStream);  
       
                    string responseString = responseReader.ReadToEnd();  
       
                    // Close response object.  
                    webResponse.Close();  
       
                }  
                catch (Exception)  
                { }  
            }  
     
    Sunday, July 20, 2008 8:35 PM

Answers

  • www-authenticate header is present. Basic realm="Portal Agent"

    Does this "Portal Agent" make any sense to you? Do you know where it is configured? It could be through some ISAPI filter on IIS or through Proxy Server.

    You will first need to find out from where this "Portal Agent" is coming..


    Pradeep Sethi
    • Marked as answer by jack 321 Friday, July 25, 2008 2:27 AM
    Sunday, July 20, 2008 10:18 PM

All replies

  • I would suggest you to create a sample html page and upload the file by sending it through POST method to
    http://test/foto/bin/upload.dll
    and then use ieHTTPHeaders  to study the HTTP header

    The issue could be with IIS where the upload.dll is hosted.
     
    Pradeep Sethi
    Sunday, July 20, 2008 8:56 PM
  • Well I have created a web application that uploads files to the same dll and this works fine 
       <form id="form1" name="form1" method="post" 
        action="http://test/foto/bin/upload.dll" enctype="multipart/form-data">  
      
        <input type="hidden" name="username" value="tesuser" /> 
        <input type="hidden" name="password" value="123" /> 
          
        <b>Upload File: </b> 
        <input type="file" id="file" name="file" />&nbsp;  
        <input type="submit" name="Submit" value="Upload" />&nbsp;  
        </form> 

    This works just great and the www-authenticate header is not presenet.
    So I guess this means that the header is injected at my form application and not iis?
    Sunday, July 20, 2008 9:38 PM
  • Try setting HttpWebRequest.PreAuthenticate to false.

    Although the default value is false, but you don't know ..

    Check this link



    Pradeep Sethi
    • Edited by Pradeep Sethi Sunday, July 20, 2008 9:51 PM added the msdn link
    Sunday, July 20, 2008 9:48 PM
  • I have tried that. Didnt solve my problem
    Sunday, July 20, 2008 10:00 PM
  • www-authenticate header is present. Basic realm="Portal Agent"

    Does this "Portal Agent" make any sense to you? Do you know where it is configured? It could be through some ISAPI filter on IIS or through Proxy Server.

    You will first need to find out from where this "Portal Agent" is coming..


    Pradeep Sethi
    • Marked as answer by jack 321 Friday, July 25, 2008 2:27 AM
    Sunday, July 20, 2008 10:18 PM