代码之家  ›  专栏  ›  技术社区  ›  Toran Billups

向Silverlight HTTP Post Async添加数据

  •  3
  • Toran Billups  · 技术社区  · 15 年前

    我试图使用Silverlight3.0中的一个简单的HTTP帖子发送一些数据,但出于某种原因,我似乎找不到一个好的例子来说明如何做到这一点。

    到目前为止,这是可行的,但是我如何在请求的同时发送一些数据呢?

    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 回复  |  直到 11 年前
        1
  •  1
  •   AnthonyWJones    15 年前

    你需要 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 ,这是对此内容类型的数据值进行编码的正确方法。它假定服务器需要UTF-8字符编码。

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

        2
  •  1
  •   Community CDub    8 年前

    最终的解决方案如下所示。感谢@ 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
    
    推荐文章