Hello!
Here is some background:
I have a smart client app that was originally written with VS2003 compact framework 1.0....I upgraded the app to VS2005 compact framework 2.0.
I use httpwebrequest to transfer a local file from my device to a virtual folder on my server.
All code worked fine (and still works with the old app in VS2003)...however, in the new app (VS2005) it will not work. When it hits the httpwebrequest.GetResponse() I get the error: The remote server returned an error: (401) Unathorized. 
Can anyone shed some light on this for me
Here is some of my code:
'*******************************************************************************************
Dim
uploadUrl As String = "http://MyServer/MyVirtualFolder/FolderInVirtualDirectory/MyFile.txt"
Dim req As HttpWebRequest = (WebRequest.Create(uploadUrl))
req.Credentials = New NetworkCredential(pstrUserID, pstrPwd)
req.Method = "PUT"
req.AllowWriteStreamBuffering =
True
Dim reqStream As Stream = req.GetRequestStream
Dim wrtr As StreamWriter = New StreamWriter(reqStream)
Dim rdr As StreamReader = New StreamReader(localFile)
Dim inLine As String = rdr.ReadLine
While inLine <> Nothing
wrtr.WriteLine(inLine)
inLine = rdr.ReadLine
End While
rdr.Close()
wrtr.Close()
req.GetResponse()
'************************************************************************************************
Any help is greatly appreciated!!!
HttpWebRequest (401) Unauthorized
Hooper
Hi,
What is the web server you used on MyServer What security permission did you use for the directory you attempted to access
Cheers,
Anthony Wong [MSFT]
Programm3r
Yes our application is upgraded from compact framework1.0 to compact framework 2.0
I am getting the same error
My code is as below
try
{
//Our postvars
byte[] buffer = Encoding.ASCII.GetBytes("mypram " + new String('x', int.Parse(textBox1.Text)));
//Initialisation, we use localhost, change if appliable
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://www.testsite.com");
//Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST";
//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Let's show some information about the response
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
}
catch (Exception ex)
{
MessageBox.Show("Error is " + ex.Message);
}
Stuman99
Hi Kelster,
If you can post a code snippet that reproduces your issue I could investigate the issue further. One thing you may want to try is changing your web request's keep-alive setting.
Cheers,
Anthony Wong [MSFT]
Fata1Attack
RamaGudimetla
Hi Anthony,
Thanks for the reply.
I got in touch with Microsoft and had some support on this issue. The person I spoke with from Microsoft said that this was a bug in VS2005 and that it was being looked into. Apparently they thought it (the bug) was gone, but it came back. (It is not fixed in the new service pack) From what I can understand, it has to do with some security issue when crossing servers within a domain in a virtual folder; my virtual folder on serverA pointed to a shared folder on another server in my domain.
The bug comes into play when you have a brand new project in VS2005, and when you upgrade a project from VS2003 to VS2005 AND upgrade the compact framework. If you do not upgrade the compact framework on the upgraded project - your project will still work. (this is why I thought it was a bug in the compact framework...but apparently it is in VS2005 somewhere)
FYI...The person from Microsoft support did not know when this would be fixed.
Regards,
Kelster
mitasid
Hi Anthony,
Thanks for the post. My server is a Windows 2000 server running IIS 6.0. The security on the virtual directory is set to Windows Authentication and Basic with my default domain. My userid that I use has read/write permissions on the shared folder. (The virtual directory points to a shared folder on a different server running Windows Server 2003). Like I mentioned before, it works okay with my old app written in VS2003, but fails on the new app with VS2005 when it hits the GetResonse(). The device that I am using is a Windows Mobile 2003 HHP Dolphin (9551).
I did upgrade my compact framework 2.0 to SP1 with the patch today - hoping it might fix my problem (no go). I also created a new virtual directory on a different web server (that has Windows Server 2003) and tried that one. I get the same error except different code (501) Not Implemented. Everything I could find on this error says that there is pretty much nothing that you can do. So, I am stumped.
Waiting in anticipation,
Kelster
danni123
You only have 2 choices that I know of...
1: Keep project in VS2003.
2: Upgrade project to VS2005, but do not upgrade the compact framework for the project. (When you upgrade the compact framework is when you will start getting the error.)
I wanted some of the newer features of VS2005, so I opted for option 2. Who knows when the bug will be fixed
Hope this helps.
Kelster