开发者

handle StatusCode==302 when using HttpWebrequest - c#

I have a windows app that I am trying to build to simulate an upload to a web app. The project is in C# 3.0.

When using Fiddler, I can see the following

/login page开发者_JS百科 - 200 code

enter pwd/uname

/home page - 302 code /home page - 200 code

/upload page [This page has simple multi-part form post where user can select at max 2 files to upload] - 200 code

/fileprocessed page - 302 code /fileprocessed page - 200 code

When i use HttpWebrequest with webresponse object, i get

/login page - 200 code

enter pwd/uname

/home page - 200 code

/upload page [This page has simple multi-part form post where user can select at max 2 files to upload] - 200 code

/fileprocessed page - 302 code

I do have SetAutoRedirect to true

My code is

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Write(buffer, 0, buffer.Length);
                FileStream stream2 = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[0x1000];
                int count = 0;
                while ((count = stream2.Read(buffer, 0, buffer.Length)) != 0)
                {
                    requestStream.Write(buffer, 0, count);
                }
                bytes = Encoding.ASCII.GetBytes("\r\n--" + str2 + "--\r\n");
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                stream.Close();
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                if(response.StatusCode == HttpStatusCode.Found)
                {
                    string newURL = response.Headers["Location"];
                }

How do I avoid the 302 error or how do I account for it wherein I can do a successful form post to simulate the upload


I had a similar case, where I recieved 302 status codes when I expected 200 to be returned (like in Fiddle or Firebug: net tab). I was able to solve the problem by giving an user agent specification to the HttpWebResponse

request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜