Answered by:
http get request and xml response

Question
-
Hello,
My form sends an http get request, i need to get a response in string
I'm actually getting a string, but in strange symbols, i guess because the response is in xml
here's my code in vb.net
Dim sURL As String sURL = "http://web.mirsms.ru/public/http/?user=xxx&pass=xxx&action=inbox" Dim wrGETURL As WebRequest wrGETURL = WebRequest.Create(sURL) Dim myProxy As New WebProxy("myproxy", 80) myProxy.BypassProxyOnLocal = True 'wrGETURL.Proxy = myProxy 'wrGETURL.Proxy = WebProxy.GetDefaultProxy() Dim objStream As Stream objStream = wrGETURL.GetResponse.GetResponseStream() Dim objReader As New StreamReader(objStream) Dim sLine As String = "" Dim i As Integer = 0 Do While Not sLine Is Nothing i += 1 sLine = objReader.ReadLine If Not sLine Is Nothing Then MsgBox(sLine) End If Loop
Thanks in advance
- Edited by hollow82 Monday, December 10, 2012 12:57 PM
- Moved by Mark Liu-lxf Wednesday, December 12, 2012 2:28 AM (From:Visual Basic General)
Monday, December 10, 2012 11:51 AM
Answers
-
Finally i found out, that the webresponse was actually compressed with gZip.
This 0x1f at the very beginning was a sign, that a webresponse is compressed(found this information in the internet).
Here's the working code in vb.net (in case somebody'll have the same problem)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim UrlDataRequest As String Dim Request As System.Net.HttpWebRequest Dim Response As System.Net.WebResponse Dim Stream As System.IO.Stream Dim UserAgent As String UrlDataRequest = "http://web.mirsms.ru/public/http/?user=23276.1&pass=xxx&action=post_sms&message=hello&target=89817477417" UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.17 Safari/537.11" 'Download data Dim StringStreamReader As System.IO.StreamReader Dim aString As String Try Request = System.Net.WebRequest.Create(UrlDataRequest) Request.AutomaticDecompression = System.Net.DecompressionMethods.Deflate Or System.Net.DecompressionMethods.GZip Request.Headers.Add("Accept-Encoding", "gzip,deflate") Request.UserAgent = UserAgent Response = Request.GetResponse() Stream = Response.GetResponseStream() StringStreamReader = New StreamReader(Stream) aString = StringStreamReader.ReadToEnd.ToString MsgBox(aString)' finally i've got what i needed Catch ex As System.Net.WebException Throw ex End Try End Sub
Thanks everybody for the answers anyway. :)
- Marked as answer by hollow82 Saturday, December 22, 2012 3:44 PM
Saturday, December 22, 2012 3:44 PM
All replies
-
Hi,
I feel you can use XMLReader to get the stream out. But just wondering it that XML or HTML ???
Anyways, below are few good stuff to read and solve your problem.
I hope this should resolve your issue.
Cheers!
Monday, December 10, 2012 2:13 PM -
Thanks a lot for Your answer! As far as i understand i'm sending a http request and getting a xml response(or am i wrong?)
i actually tried somthing like this:
Dim sURL As String sURL = "http://web.mirsms.ru/public/http/?user=xxxx&pass=xxxxx&action=inbox" Dim wrGETURL As WebRequest wrGETURL = WebRequest.Create(sURL) Dim myProxy As New WebProxy("myproxy", 80) myProxy.BypassProxyOnLocal = True 'wrGETURL.Proxy = myProxy 'wrGETURL.Proxy = WebProxy.GetDefaultProxy() Dim objStream As Stream objStream = wrGETURL.GetResponse.GetResponseStream() Dim objReader As New Xml.XmlTextReader(objStream) Dim sLine As String = "" Dim i As Integer = 0 objReader.MoveToContent() ' Parse the file and display each of the nodes. While objReader.Read() Select Case objReader.NodeType Case Xml.XmlNodeType.Element Console.Write("<{0}>", objReader.Name) Case Xml.XmlNodeType.Text Console.Write(objReader.Value) Case Xml.XmlNodeType.CDATA Console.Write("<![CDATA[{0}]]>", objReader.Value) Case Xml.XmlNodeType.ProcessingInstruction Console.Write("<?{0} {1}?>", objReader.Name, objReader.Value) Case Xml.XmlNodeType.Comment Console.Write("<!--{0}-->", objReader.Value) Case Xml.XmlNodeType.XmlDeclaration Console.Write("<?xml version='1.0'?>") Case Xml.XmlNodeType.Document Case Xml.XmlNodeType.DocumentType Console.Write("<!DOCTYPE {0} [{1}]", objReader.Name, objReader.Value) Case Xml.XmlNodeType.EntityReference Console.Write(objReader.Name) Case Xml.XmlNodeType.EndElement Console.Write("</{0}>", objReader.Name) End Select End While
and always got a '' hexadecimal value 0x1F, is an invalid character string 1 position 1 error onobjReader.MoveToContent()
i'm kinda new to this theme and its quite hard to understand
- Edited by hollow82 Monday, December 10, 2012 2:52 PM
Monday, December 10, 2012 2:48 PM -
0x1F is a Ctrl-_ character in position 1 (or is it 2) of you input file. Check your input and verify that there are no invalide characters present.
"Premature optimization is the root of all evil." - Knuth
If I provoked thought, please click the green arrow
If I provoked Aha! please click Propose as Answer
We are here to learn, to share knowledge, and to earn points; all in about equal measure.
Monday, December 10, 2012 3:07 PM -
I went to that website with your URL and received this
<?xml version="1.0" encoding="UTF-8"?> -<output> <RECEIVER DATE_REPORT="10.12.2012 19:15:19" AGT_ID=""/> -<errors><error code="-20107">Ошибка авторизации xxx</error></errors></output>
although when I use your code to get the response I just get a couple of strange characters.You've taught me everything I know but not everything you know.
Monday, December 10, 2012 3:18 PM -
Hi Hollow82,
1. Generally this kind of errors are due to XSL format specially in the first line. If you are using XSL, check the xsl to make sure that there is no space in the first position of line 1. Otherwise, paste the complete XSL file here.
2. You can use HttpUtility.HtmlEncode for your strings which you are getting response.
Refer more for HttpUtility.HtmlEncode
Remark from MSDN on this kind of issues :
If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. For example, when embedded in a block of text, the characters < and > are encoded as < and > for HTTP transmission.
Cheers!
Monday, December 10, 2012 3:22 PM -
If i make the same request
http://web.mirsms.ru/public/http/?user=xxxx&pass=xxxxx&action=inbox
in google chrome browser
i get this in response
<output><RECEIVER AGT_ID="" DATE_REPORT="10.12.2012 19:18:43"/><errors><error code="-20107">Ошибка авторизации xxxx</error></errors></output>(It's an authorization error cause i don't wanna display my pass and login, but still it's a normal response...)
so the browser understands a response and xmlTextReader finds an error in the xml response?
- Edited by hollow82 Monday, December 10, 2012 3:35 PM
Monday, December 10, 2012 3:24 PM -
Strange...
The actions that are performed by this http request(checking the sms inbox, sending sms) are working from my code but i really need to see the response...
Monday, December 10, 2012 4:01 PM -
Hi Hollow,
Welcome to the MSDN forum.
There is a specific forum to deal with the network issue. For better support, I will move this thread to Network Class Library (System.Net) forum. It will cost a little time to involve the members in this forum. I appreciate your patience.
Sorry for any incontinence and have a nice day.
Mark Liu-lxf
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Wednesday, December 12, 2012 2:27 AM -
Finally i found out, that the webresponse was actually compressed with gZip.
This 0x1f at the very beginning was a sign, that a webresponse is compressed(found this information in the internet).
Here's the working code in vb.net (in case somebody'll have the same problem)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim UrlDataRequest As String Dim Request As System.Net.HttpWebRequest Dim Response As System.Net.WebResponse Dim Stream As System.IO.Stream Dim UserAgent As String UrlDataRequest = "http://web.mirsms.ru/public/http/?user=23276.1&pass=xxx&action=post_sms&message=hello&target=89817477417" UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.17 Safari/537.11" 'Download data Dim StringStreamReader As System.IO.StreamReader Dim aString As String Try Request = System.Net.WebRequest.Create(UrlDataRequest) Request.AutomaticDecompression = System.Net.DecompressionMethods.Deflate Or System.Net.DecompressionMethods.GZip Request.Headers.Add("Accept-Encoding", "gzip,deflate") Request.UserAgent = UserAgent Response = Request.GetResponse() Stream = Response.GetResponseStream() StringStreamReader = New StreamReader(Stream) aString = StringStreamReader.ReadToEnd.ToString MsgBox(aString)' finally i've got what i needed Catch ex As System.Net.WebException Throw ex End Try End Sub
Thanks everybody for the answers anyway. :)
- Marked as answer by hollow82 Saturday, December 22, 2012 3:44 PM
Saturday, December 22, 2012 3:44 PM