UnAuthExp in FORM submit thru IE  
Author Message
NRJ





PostPosted: .NET Framework Networking and Communication, UnAuthExp in FORM submit thru IE Top

Hi,

My program has to sumbit some data to a website and read the result form it. This process is to be repeated continuously using different sets of data.

My problem is that when data is submitted using the following code, it produces an UnauthorizedAccessException at line 70 (htmForm.submit();) when the for loop is in 3rd or 4th iteration, and it never recovers. If I run this in debug mode pressing F10 line by line, it never occurs. This code runs in a separate thread.

Thread t = new Thread(new ParameterizedThreadStart(new IE7().doWork(sLinNames, sURL)));

I have tried increasing timeout etc. It is not working. I am running this code in Admin account on Win XP-SP2 machine with IE7, C# 2005.

Please help in solving this problem.

/******************* CODE START *********************************/

using System;

using System.IO;

using mshtml;

using SHDocVw;

using System.Threading;

using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace IEcontroller

{

class IE7

{

private InternetExplorer _IE;

private int _timeoutSeconds = 30;

private bool isDocumentComplete = false;

static AutoResetEvent documentComplete = new AutoResetEvent(false);

private IWebBrowserApp IE

{

get { return (IWebBrowserApp)_IE; }

}

public IE7()

{

_IE = new InternetExplorerClass();

IE.Visible = true;

}

private void WaitForComplete()

{

int elapsedSeconds = 0;

while (!isDocumentComplete & elapsedSeconds >= TimeoutSeconds)

{

Thread.Sleep(1000);

elapsedSeconds++;

}

documentComplete.WaitOne(_timeoutSeconds * 100, false);

}

public int TimeoutSeconds

{

get { return _timeoutSeconds; }

set { _timeoutSeconds = value; }

}

public void doWork(String[] sLoginNames, String sURL )

{

int iStartAmt = 1;

object empty = "";

HTMLFormElement htmForm = null;

foreach ( String s in sLoginNames )

{

IE.Navigate(sURL, ref empty, ref empty, ref empty, ref empty);

WaitForComplete();

while (htmForm == null)

{

htmForm = (HTMLFormElement)getElementById("frm_submit");

}

++iStartAmt;

setInputStringValue("quote", iStartAmt.ToString());

setInputStringValue("loginname", s);

getElementById("frm_submit").scrollIntoView("");

WaitForComplete();

try

{

htmForm.submit();

}

catch (UnauthorizedAccessException uae)

{

// Cleanup here

}

//readResponse(htmForm);

}

}

private void IE_DocumentComplete(object pDisp, ref object URL)

{

isDocumentComplete = true;

documentComplete.Set();

}

private void IE_ProgressChange(int Progress, int ProgressMax)

{

isDocumentComplete = (Progress < ProgressMax) false : true;

}

public void SetInputStringValue(string inputId, string elementValue)

{

HTMLInputElementClass input = getInputElement(inputId);

input.value = elementValue;

}

private HTMLInputElementClass getInputElement(string inputId)

{

return (HTMLInputElementClass)getElementById(inputId);

}

public IHTMLElement getElementById(string elementId)

{

try

{

HTMLDocument document = ((HTMLDocument)IE.Document);

IHTMLElement element = document.getElementById(elementId);

int nullElementCount = 0;

// The following loop is to account for any latency that IE

// might experience. Tweak the number of times to attempt

// to continue checking the document before giving up.

while (element == null && nullElementCount < 10)

{

Thread.Sleep(500);

element = document.getElementById(elementId);

nullElementCount++;

}

return element;

} catch ( COMException ce) {

MessageBox.Show(ce.StackTrace, "COMException", MessageBoxButtons.OK, MessageBoxIcon.Error);

return null;

}

}

public void setInputStringValue(string inputId, string elementValue)

{

HTMLInputElementClass input = getInputElement(inputId);

input.value = elementValue;

}

}

}

/********************* CODE END ************************************/



.NET Development17