Quantcast
Channel: Returned string from responseStream
Viewing all articles
Browse latest Browse all 2

Re: Returned string from responseStream

$
0
0

Hi jula,

jula

Returned string from responseStream

As for this issue, I suggest you could refer to the following code:

    Implements IMyService
    Public Function PostSampleMethod(ByVal data As Stream) As String Implements IMyService.PostSampleMethod
        'convert Stream Data to StreamReader
        Dim reader = New StreamReader(data)
        'Read StreamReader data as string
        Dim xmlString As String = reader.ReadToEnd()
        Dim returnValue As String = xmlString
        'return the XMLString data
        Return returnValue
    End Function

    Public Function GetSampleMethod(ByVal strUserName As String) As String Implements IMyService.GetSampleMethod
        Dim strReturnValue As StringBuilder = New StringBuilder()
        'return username prefixed as shown below
        strReturnValue.Append(String.Format("You have entered userName as {0}", strUserName))
        Return strReturnValue.ToString()
    End Function

Code in page (.aspx.vb)

   Protected Sub Button1_Click(sender As Object, e As EventArgs)

        'Restful service URL
        Dim url As String = "http://localhost:45822/MyService.svc/GetSampleMethod/inputStr/suryaprakash"
        Dim strResult As String = String.Empty
        ' declare httpwebrequet wrt url defined above
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)

        ' set method as post
        request.Method = "GET"
        ' set content type
        request.ContentType = "application/x-www-form-urlencoded"
        ' declare & read response from service
        Dim webresponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

        ' set utf8 encoding
        Dim enc As Encoding = System.Text.Encoding.GetEncoding("utf-8")
        ' read response stream from response object
        Dim loResponseStream As StreamReader = New StreamReader(webresponse.GetResponseStream(), enc)


        ' read string from stream data
        strResult = loResponseStream.ReadToEnd()
        ' close the stream object
        loResponseStream.Close()
        ' close the response object
        webresponse.Close()
        ' assign the final result to text box
        lblResult.Text = strResult

    End Sub

For more details, please refer to the following article: http://www.codeproject.com/Articles/275279/Developing-WCF-Restful-Services-with-GET-and-POST

Best regards,
Dillion


Viewing all articles
Browse latest Browse all 2

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>