Click here to Skip to main content
16,011,647 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: webservice and SQL Server 2008 Pin
Richard MacCutchan12-Mar-13 6:54
mveRichard MacCutchan12-Mar-13 6:54 
QuestionRemote File Upload Through ASP .Net page Pin
Vodstok11-Mar-13 5:54
Vodstok11-Mar-13 5:54 
QuestionRe: Remote File Upload Through ASP .Net page Pin
jkirkerx11-Mar-13 8:26
professionaljkirkerx11-Mar-13 8:26 
AnswerRe: Remote File Upload Through ASP .Net page Pin
Vodstok11-Mar-13 8:31
Vodstok11-Mar-13 8:31 
GeneralRe: Remote File Upload Through ASP .Net page Pin
jkirkerx11-Mar-13 9:56
professionaljkirkerx11-Mar-13 9:56 
AnswerRe: Remote File Upload Through ASP .Net page Pin
Richard Deeming11-Mar-13 9:13
mveRichard Deeming11-Mar-13 9:13 
GeneralRe: Remote File Upload Through ASP .Net page Pin
Vodstok11-Mar-13 9:26
Vodstok11-Mar-13 9:26 
GeneralRe: Remote File Upload Through ASP .Net page Pin
Vodstok12-Mar-13 4:20
Vodstok12-Mar-13 4:20 
Okay, so I have a question; I am doing a get, scraping out the event validation and viewstate to send back, but when I try to post back to the page, I am getting what appears to be a non-posted page on the other end.

Since I can't seem to reuse the original request, the page I get has different viewstate information, so I'm not getting the post I hoped (I think that is what is happening anyway). I am definitely hitting the page; I am doing a Response.Write for the returned content and it is definitely loading the correct page in the background.

I seem to be back at square one (albeit using the baked-in features of 4.5, which is nice Smile | :) ). So I am definitely calling the right page, but it does not appear to actually post.

Here is the code I am using:
C#
//First call to get the hidden fields from the page
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://localhost:51749/Default.aspx");
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

string respString = string.Empty;

using (Stream strm = resp.GetResponseStream())
{
    using (StreamReader sr = new StreamReader(strm))
    {
        respString = sr.ReadToEnd();
    }
}
request = (HttpWebRequest)HttpWebRequest.Create(@"http://localhost:51749/Default.aspx");
List<string> controls = respString.Split('\n').Select(o => o.Replace("\r", string.Empty).Trim()).Where(o => o.StartsWith("<input") && o.IndexOf("hidden") > -1).ToList();


request.Method = "POST";
request.ContentType = "multipart/form-data";


//copied from StackOverflow and updated to hit my URL
using (HttpClient client = new HttpClient())
{
    using (MultipartFormDataContent content = new MultipartFormDataContent())
    {
        var values = new[]
                        {
                            new KeyValuePair<string, string>("txtFile", "BALHHHHH"),
                            new KeyValuePair<string, string>("fu1", fileName),
                        }.ToList();

        foreach (string cont in controls)
        {
            List<string> attributes = cont.Split(' ').Where(o => o.ToLower().StartsWith("id") || o.ToLower().StartsWith("value")).Select(o => o.Replace("\"", string.Empty)).ToList();

            string id = attributes.Where(o => o.ToLower().StartsWith("id")).FirstOrDefault().Split('=')[1];
            string value = attributes.Where(o => o.ToLower().StartsWith("value")).FirstOrDefault().Split('=')[1];

            values.Add(new KeyValuePair<string, string>(id, value));
        }


        foreach (KeyValuePair<string, string> keyValuePair in values)
        {
            content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
        }

        ByteArrayContent fileContent = new ByteArrayContent(File.ReadAllBytes(fileName));
        fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = Path.GetFileName(fileName)
        };
        content.Add(fileContent);

        string requestUri = "http://localhost:51749/Default.aspx";
        HttpResponseMessage result = client.PostAsync(requestUri, content).Result;

        using (Stream res = await result.Content.ReadAsStreamAsync())
        {
            byte[] bres = new byte[res.Length];
            res.Read(bres, 0, bres.Length);
            Response.Write(Encoding.ASCII.GetString(bres));

        }


    }
}


I think at this point any suggestion would be a good one. I was using older techniques before and achieving the same thing, and Richard's suggestion seems to be the first thing I have encountered that offered a solution as to why what I was doing before wasn't working, so I suspect something in here is to blame.
______________________
Oh Hamburgers!

GeneralRe: Remote File Upload Through ASP .Net page Pin
Richard Deeming12-Mar-13 4:36
mveRichard Deeming12-Mar-13 4:36 
GeneralRe: Remote File Upload Through ASP .Net page Pin
Vodstok12-Mar-13 5:13
Vodstok12-Mar-13 5:13 
GeneralRe: Remote File Upload Through ASP .Net page Pin
Vodstok14-Mar-13 4:27
Vodstok14-Mar-13 4:27 
QuestionASP.Net MVC 4? Pin
silentspeaker11-Mar-13 3:38
silentspeaker11-Mar-13 3:38 
AnswerRe: ASP.Net MVC 4? Pin
Sandeep Mewara11-Mar-13 4:16
mveSandeep Mewara11-Mar-13 4:16 
AnswerRe: ASP.Net MVC 4? Pin
Thomas Daniels11-Mar-13 8:40
mentorThomas Daniels11-Mar-13 8:40 
Questionrunat=server Pin
tiwal11-Mar-13 0:47
tiwal11-Mar-13 0:47 
AnswerRe: runat=server Pin
Deflinek11-Mar-13 4:24
Deflinek11-Mar-13 4:24 
AnswerRe: runat=server Pin
Sandeep Mewara11-Mar-13 4:26
mveSandeep Mewara11-Mar-13 4:26 
AnswerRe: runat=server Pin
jkirkerx11-Mar-13 9:06
professionaljkirkerx11-Mar-13 9:06 
Questionquery regarding session nature Pin
Tridip Bhattacharjee11-Mar-13 0:28
professionalTridip Bhattacharjee11-Mar-13 0:28 
AnswerRe: query regarding session nature Pin
Keith Barrow11-Mar-13 0:54
professionalKeith Barrow11-Mar-13 0:54 
GeneralRe: query regarding session nature Pin
Tridip Bhattacharjee11-Mar-13 1:07
professionalTridip Bhattacharjee11-Mar-13 1:07 
GeneralRe: query regarding session nature Pin
Keith Barrow11-Mar-13 1:22
professionalKeith Barrow11-Mar-13 1:22 
GeneralRe: query regarding session nature Pin
Tridip Bhattacharjee11-Mar-13 4:49
professionalTridip Bhattacharjee11-Mar-13 4:49 
GeneralRe: query regarding session nature Pin
Keith Barrow11-Mar-13 5:02
professionalKeith Barrow11-Mar-13 5:02 
QuestionNeed to crawl a Microsoft WebPage which live authenticated... Pin
Mutturaj Bali10-Mar-13 23:25
Mutturaj Bali10-Mar-13 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.