Silverlight POST Request

  • Silverlight

    Silverlight POST Request

    Abend,

    ich versuche derzeit einen einfachen POST in Silverlight durchzuführen, habe jedoch starke Probleme bei der Fehlersuche. Den Code habe ich von der Mircosoft Seite.

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.Net
    3. Imports System.IO
    4. Imports System.Text
    5. Imports System.Threading
    6. Imports Microsoft.VisualBasic
    7. Class HttpWebRequestBeginGetRequest
    8. 'Public Shared allDone As New ManualResetEvent(False)
    9. Public Shared Sub Main()
    10. ' Create a new HttpWebRequest object.
    11. Dim request As HttpWebRequest = CType(WebRequest.Create("http://testseite.de/test.php"), _
    12. HttpWebRequest)
    13. ' Set the ContentType property.
    14. request.ContentType = "application/x-www-form-urlencoded"
    15. ' Set the Method property to 'POST' to post data to the URI.
    16. request.Method = "POST"
    17. ' Start the asynchronous operation.
    18. Dim result As IAsyncResult = _
    19. CType(request.BeginGetRequestStream(AddressOf GetRequestStreamCallback, request), _
    20. IAsyncResult)
    21. ' Keep the main thread from continuing while the asynchronous
    22. ' operation completes. A real world application
    23. ' could do something useful such as updating its user interface.
    24. 'allDone.WaitOne()
    25. End Sub ' Main
    26. Private Shared Sub GetRequestStreamCallback(ByVal asynchronousResult As IAsyncResult)
    27. Dim request As HttpWebRequest = CType(asynchronousResult.AsyncState, HttpWebRequest)
    28. ' End the operation
    29. Dim postStream As Stream = request.EndGetRequestStream(asynchronousResult)
    30. Dim postData As [String] = "user=asdf"
    31. ' Convert the string into byte array.
    32. Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    33. ' Write to the stream.
    34. postStream.Write(byteArray, 0, postData.Length)
    35. postStream.Close()
    36. ' Start the asynchronous operation to get the response
    37. Dim result As IAsyncResult = _
    38. CType(request.BeginGetResponse(AddressOf GetResponseCallback, request), _
    39. IAsyncResult)
    40. End Sub ' ReadRequestStreamCallback
    41. Private Shared Sub GetResponseCallback(ByVal asynchronousResult As IAsyncResult)
    42. Dim request As HttpWebRequest = CType(asynchronousResult.AsyncState, HttpWebRequest)
    43. ' Get the response.
    44. Dim response As HttpWebResponse = CType(request.EndGetResponse(asynchronousResult), _
    45. HttpWebResponse)
    46. Dim streamResponse As Stream = response.GetResponseStream()
    47. Dim streamRead As New StreamReader(streamResponse)
    48. Dim responseString As String = streamRead.ReadToEnd()
    49. MessageBox.Show(responseString)
    50. ' Close Stream object.
    51. streamResponse.Close()
    52. streamRead.Close()
    53. ' Release the HttpWebResponse.
    54. 'allDone.Set()
    55. response.Close()
    56. End Sub ' ReadResponseCallback
    57. End Class ' HttpWebRequest_BeginGetRequest


    Aufrufen tue ich das ganze über "HttpWebRequestBeginGetRequest.Main()"
    Es tut sich leider nur wenig, habe mit Messageboxen geschaut wie weit das ganze läuft und nach dem Main() Sub war schon Schluss.