POSTING DATA using HttpWebRequest (HELP) 
Author Message
 POSTING DATA using HttpWebRequest (HELP)

Win2000
.NET BETA 2
C# Windows Application

hi,

I am looking for a sample where you can set variables to a HttpWebRequest.
I found ways to do it with WebClient and the QueryString property but
strangely i could  not find similar property in the HttpWebRequest.

What I am trying to do is upload a file with some value/pair property being
part of the request (similar to a dubmit in a form).

Thanks in advance
Dalmer



Sun, 21 Mar 2004 07:10:01 GMT  
 POSTING DATA using HttpWebRequest (HELP)


Quote:
> Win2000
> .NET BETA 2
> C# Windows Application

> hi,

> I am looking for a sample where you can set variables to a HttpWebRequest.
> I found ways to do it with WebClient and the QueryString property but
> strangely i could  not find similar property in the HttpWebRequest.

See:

http://www.gotdotnet.com/quickstart/howto/doc/WebRequests/clientGET.aspx

--
Patrick Steele



Sun, 21 Mar 2004 20:22:55 GMT  
 POSTING DATA using HttpWebRequest (HELP)

Here is the code to upload to the ASPX file. First read the data into a byte
array:

Private Function UploadToWeb(ByVal data As Byte(), ByVal destinationURI As
String) As Boolean

      '-----------------------------------------------------------

      ' Uploads serialized data to web server

      '-----------------------------------------------------------

      Dim reqStream As Stream

      Dim strRes As Stream

      Dim hWebResp As WebResponse

      Dim hWebReq As HttpWebRequest ' WebRequest

      Try

            '-- create the Request object

            hWebReq = System.Net.HttpWebRequest.Create(destinationURI)
http://www.company.com/upload.aspx

            '-- set WebRequest properties

            With hWebReq

                  Dim cred As NetworkCredential = New
NetworkCredential("alias", "password", "domain")

                  .Credentials = cred

                  .Timeout = System.Threading.Timeout.Infinite          '--
optional; default is 100 s

                  .Method = "POST"

                  .ContentType = "application/octet-stream"

                  .ContentLength = data.Length

                  reqStream = .GetRequestStream

            End With

            '-- get the request stream and write the data

            With reqStream

                  .Write(data, 0, data.Length)

                  .Close()

            End With

            '-- get response

            hWebResp = hWebReq.GetResponse

            With hWebResp

                  '-- read the response into byte array

                  Dim strArr(9999) As Byte

                  strRes = hWebResp.GetResponseStream

                  strRes.Read(strArr, 0, strArr.Length)

                  strRes.Close()

                  '-- convert response to string

                  Dim strResult As String =
Encoding.ASCII.GetString(strArr).Trim

                  '-- parse response

                  Dim iPos As Integer = strResult.IndexOf("<!DOCTYPE")

                  If iPos > 0 Then strResult = strResult.Substring(0,
iPos).Trim

                  '-- send notification to re-enroll if not successful

                  If Not strResult.ToUpper.Equals("SUCCESS") Then

                        Throw New Exception(strResult)

                  End If

            End With

            Return True

      Catch e As Exception

            Throw e

      Finally

            If Not (strRes Is Nothing) Then strRes.Close()

            If Not (reqStream Is Nothing) Then reqStream.Close()

            If Not (hWebReq Is Nothing) Then hWebReq = Nothing

            If Not (hWebResp Is Nothing) Then hWebResp = Nothing

      End Try

End Function

Here is the code in the ASPX file to process the received data:

      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

            '----------------------------------------------------------

            ' Process posted data

            '----------------------------------------------------------

            Try

                  If Not IsPostBack Then

                        With Request

                              If .TotalBytes > 0 Then       '-- check that
there is data posted

                                    Dim bodyArr As Byte() =
.BinaryRead(.TotalBytes)                '-- get the posted data

                                    If ProcessSucceded > 0 Then

                                          Response.Write("Success")

                                    Else

                                          Response.Write("Failed!")

                                    End If

                              End If

                        End With

                  End If

            Catch ex As Exception

                  Response.Write(ex.Message)

            Finally

            End Try

      End Sub

---------------------------

Note that this code runs correctly only in post beta 2 builds.

Taiwo


Quote:
> Win2000
> .NET BETA 2
> C# Windows Application

> hi,

> I am looking for a sample where you can set variables to a HttpWebRequest.
> I found ways to do it with WebClient and the QueryString property but
> strangely i could  not find similar property in the HttpWebRequest.

> What I am trying to do is upload a file with some value/pair property
being
> part of the request (similar to a dubmit in a form).

> Thanks in advance
> Dalmer



Mon, 22 Mar 2004 13:21:58 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. httpwebrequest and posting multipart form-data

2. HttpWebRequest POST 405 Error?

3. Parse an HTTPWebResponse to post an HTTPWebRequest

4. data transfer using POST method

5. POST form data to an ASP page using the CHttpFile member SendRequest

6. Posting data to an ASP file using MFC

7. HttpWebRequest and Multipart/form-data

8. using System.net.HttpWebRequest

9. help with post using MFC

10. HELP: POST using MFC

11. HELP - Using MFC do to secure HTTPS Post

12. POST via HttpWebRequest not working. HELP!

 

 
Powered by phpBB® Forum Software