将数据添加到silverlight HTTP POST异步

时间:2009-10-27 19:51:05

标签: silverlight silverlight-3.0 http-post

我正在尝试使用Silverlight 3.0中的简单HTTP POST发送一些数据,但出于某种原因,我似乎找不到如何做到这一点的好例子。

到目前为止这是有效的,但我如何随请求发送一些数据?

Public Sub SubmitMessage()
    request = WebRequestCreator.ClientHttp.Create(New System.Uri("http://localhost:27856/Home.aspx/PostUpdate/"))
    request.Method = "POST"

    Dim result As IAsyncResult = request.BeginGetResponse(New AsyncCallback(AddressOf UpdateDone), Nothing)
End Sub

Public Sub UpdateDone(ByVal ar As IAsyncResult)
    Dim response As HttpWebResponse = request.EndGetResponse(ar)

    Using reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim valid As String = reader.ReadToEnd()
    End Using
End Sub

2 个答案:

答案 0 :(得分:1)

在获得响应之前需要BeginGetRequestStream方法(注意VB.NET不是我的主要语言)。

Public Sub SubmitMessage()
    request = WebRequestCreator.ClientHttp.Create(New System.Uri("http://localhost:27856/Home.aspx/PostUpdate/"))
    request.Method = "POST"

    request.BeginGetRequestStream(New AsyncCallback(AddressOf SendData), Nothing)
End Sub

Public Sub SendData(ByVal ar As IAsyncResult)
    Dim stream as Stream = state.Request.EndGetRequestStream(ar)
    '' # Pump your data as bytes into the stream.
    stream.Close()
    request.BeingGetResponse(New AsyncCallback(AddressOf UpdateDone), Nothing)
End Sub

Public Sub UpdateDone(ByVal ar As IAsyncResult)
    Dim response As HttpWebResponse = request.EndGetResponse(ar)

    Using reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim valid As String = reader.ReadToEnd()
    End Using
End Sub

从您的回答中可以看出,这是发布HTML表单数据。以下是创建实体主体的正确方法(C#抱歉): -

    public static string GetUrlEncodedData(Dictionary<string, string> data)
    {
        var sb = new StringBuilder();
        foreach (var kvp in data)
        {
            if (sb.Length > 0) sb.Append("&");
            sb.Append(kvp.Key);
            sb.Append("=");
            sb.Append(Uri.EscapeDataString(kvp.Value));
        }
        return sb.ToString();
    }

特别注意Uri.EscapeDataString的使用,这是对此Content-Type编码数据值的正确方法。它假定服务器期望UTF-8字符编码。

将结果转换为准备发布的字节数组时,只需使用ASCII编码,因为返回的字符串只包含ASCII范围内的字符。

答案 1 :(得分:1)

最终解决方案如下所示。感谢@ AnthonyWJones的快速回答让我朝着正确的方向前进!

Public Sub SubmitMessage()
    Dim request As HttpWebRequest = WebRequestCreator.ClientHttp.Create(New System.Uri("http://localhost:27856/Home.aspx/PostUpdate/"))
    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"

    request.BeginGetRequestStream(New AsyncCallback(AddressOf SendStatusUpdate), request)
End Sub

Public Sub SendStatusUpdate(ByVal ar As IAsyncResult)
    Dim request As HttpWebRequest = DirectCast(ar.AsyncState, HttpWebRequest)

    Dim stream As Stream = request.EndGetRequestStream(ar)

    Dim data As New System.Text.UTF8Encoding
    Dim byteArray As Byte() = data.GetBytes("status=test")

    stream.Write(byteArray, 0, byteArray.Length)
    stream.Close()
    stream.Dispose()

    request.BeginGetResponse(New AsyncCallback(AddressOf StatusUpdateCompleted), request)
End Sub

Public Sub StatusUpdateCompleted(ByVal ar As IAsyncResult)
    Dim request As HttpWebRequest = DirectCast(ar.AsyncState, HttpWebRequest)

    Dim response As HttpWebResponse = request.EndGetResponse(ar)

    Using reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim valid As String = reader.ReadToEnd()
    End Using
End Sub