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
- 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)
- { }
- }