Help with HttpWebRequest.Method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Thanks in advance for any help offered.

I am trying to fill out a form on a web page and submit the form data to the
web server , all from within my vb program.

The html sorce ( reduced for this post is)

<FORM ACTION="a_valid_url.cfm" METHOD="POST">
<BR>
<P>Trend
<SELECT NAME="trend_id">
<OPTION VALUE="ALL;ALL">ALL
<OPTION VALUE="FIRST;abc">First
<OPTION VALUE="SECOND;abc">Second
</SELECT>
<P>
<TABLE WIDTH=400>
<TR ALIGN=CENTER><TD>Month
<SELECT NAME="month">
<OPTION VALUE="ALL" SELECTED>ALL
<OPTION VALUE="1">Jan
<OPTION VALUE="2">Feb
</SELECT>
</TD>
<TD>Day
<SELECT NAME="DAY">
<OPTION VALUE="ALL">ALL
<OPTION VALUE="1">1
<OPTION VALUE="2">2
</SELECT>
</TD>
</TR>
</TABLE>
<P><INPUT TYPE="submit" VALUE="Search">
</FORM>

In my vb program I set up a HttpWebRequest as follows.
I have no experience with HTML or Jscript. Limited experience with VB.

I want to submit a trend_id of "FIRST" with "ALL" months and "ALL" days from
my vb prog.

Dim STE As String = "http://www.another_valid_url"

Dim m_cred As New NetworkCredential("username", "password")
Dim m_Cache As New CredentialCache()

m_Cache.Add(New Uri(STE), "Basic", m_cred)
m_Cache.Add(New Uri(STE), "Digest", m_cred)
m_Cache.Add(New Uri(STE), "NTLM", m_cred)
m_Cache.Add(New Uri(STE), "Kerberos", m_cred)

Dim mm_req As HttpWebRequest = CType(WebRequest.Create(STE),
HttpWebRequest)

mm_req.Credentials = m_Cache

Dim srch As String

srch = "I'm not sure what goes here??"
Dim qq() As Byte = Encoding.ASCII.GetBytes(srch)
mm_req.Method = "POST"
mm_req.ContentLength = qq.Length
mm_req.Timeout = 60000

Dim req_stream As Stream = mm_req.GetRequestStream
req_stream.Write(qq, 0, qq.Length)
req_stream.Close()

Dim mm_resp As HttpWebResponse = CType(mm_req.GetResponse(),
HttpWebResponse)

Dim readStream As New StreamReader(mm_resp.GetResponseStream,
Encoding.ASCII)
Dim ch As String = readStream.ReadToEnd
...
...
readStream.Close()
mm_resp.Close()


I have tried various ways of constructing 'srch' but I keep getting back the
unfilled form. The server doesnt proceed to the url in the Action field of
the form. The username & password are correct.


Thanks anyone
 
My first question is why are you circumventing the normal operation of
ASP.NET and creating a handler in this manner? I am not criticizing the use
of a handler, if necessary, but I find very few cases where this is necessary.

In this instance, it appears you desire the following:

1. Submit form
2. Handler grabs request and processes the form collection
3. Handler passes off to a cfm page for response

While this appears as simple as creating a post event handlerin CodeBehind,
it is much more complex. There are many moving parts. As part of the system
is out of your control (in the IDE), it is also difficult to debug.
Compounding this is your admitted lack of skill in VB, HTML, etc.

In ASP.NET, you have built in infrastructure to handle form submit. There
are times you have to step outside of that box, but you should be careful
about doing it. Make sure there is a really good reason to build your own
before building.

The first step outside of the box would be a submission to the ColdFusion
page and let it handle things, if your core competency is ColdFusion. It is
not ideal, but it can work.

Overall, when I circumvent the normal page submit, my focus is on setting up
an HttpHandler or HttpModule and installing it on the machine. This, in and
of itself, is a bit harder to debug. I will make sure the handler/module does
not contain a lot of page specific code. This is necessary for my sanity.

If you need to handle page requests dynamically, you can look at a
model-view-controller pattern, but this still does not solve the "passing off
to ColdFusion" part of the problem.

In short: Is it really necessary to pass off to ColdFusion? If yes, is it
absolutely necessary to handle the request before passing it off? Think
through these two questions very carefully, as the entire architecture of
your app is at stake.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Roger said:
Thanks in advance for any help offered.

I am trying to fill out a form on a web page and submit the form data
to the web server , all from within my vb program.

The html sorce ( reduced for this post is)

<FORM ACTION="a_valid_url.cfm" METHOD="POST">
<BR>
<P>Trend
<SELECT NAME="trend_id">
<OPTION VALUE="ALL;ALL">ALL
<OPTION VALUE="FIRST;abc">First
<OPTION VALUE="SECOND;abc">Second
</SELECT>
<P>
<TABLE WIDTH=400>
<TR ALIGN=CENTER><TD>Month
<SELECT NAME="month">
<OPTION VALUE="ALL"
SELECTED>ALL <OPTION
VALUE="1">Jan <OPTION
VALUE="2">Feb </SELECT>
</TD>
<TD>Day
<SELECT NAME="DAY">
<OPTION VALUE="ALL">ALL
<OPTION VALUE="1">1
<OPTION VALUE="2">2
</SELECT>
</TD>
</TR>
</TABLE>
<P><INPUT TYPE="submit" VALUE="Search">
</FORM>

In my vb program I set up a HttpWebRequest as follows.
I have no experience with HTML or Jscript. Limited experience with VB.

I want to submit a trend_id of "FIRST" with "ALL" months and "ALL"
days from my vb prog.

Dim STE As String = "http://www.another_valid_url"

Dim m_cred As New NetworkCredential("username", "password")
Dim m_Cache As New CredentialCache()

m_Cache.Add(New Uri(STE), "Basic", m_cred)
m_Cache.Add(New Uri(STE), "Digest", m_cred)
m_Cache.Add(New Uri(STE), "NTLM", m_cred)
m_Cache.Add(New Uri(STE), "Kerberos", m_cred)

Setting four different authentication schemes isn't necessary. Use the
one that is really being used ;-)

Dim mm_req As HttpWebRequest = CType(WebRequest.Create(STE),
HttpWebRequest)

mm_req.Credentials = m_Cache

Dim srch As String

srch = "I'm not sure what goes here??"

You have to create a proper HTLM form query string, i.e.
fieldName1=fieldValue1&fieldName2=fieldValue2&...&fieldName<n>=fieldValu
e<n>

See http://www.w3.org/TR/html4/interact/forms.html#h-17.13.3 for
details.

So what you need is string like
"trend_id=1&month=all&DAY=all"

Cheers,
 
Back
Top