为什么第二个请求失败?我可以更改RestSharp标头的顺序吗?

时间:2016-03-16 21:06:09

标签: .net vb.net http soap restsharp

我尝试与我的家庭模式/路由/网关接口,该网络具有使用CWMP(TR-69)SOAP协议(通过javascript客户端)管理配置的Web UI。

我正在尝试使用RestSharp复制js客户端完成的第一个请求(并不需要完全相同,但只是为了工作),但它总是因500内部错误而失败。

这是来自javascript的RAW请求:

POST http://vodafone.station/data_model.cgi HTTP/1.1
Content-Type: text/xml; charset="utf-8"
Accept: application/xml, text/xml, */*; q=0.01
Method: POST
SOAPAction: cwmp:LoggedUser
X-Requested-With: XMLHttpRequest
Referer: http://vodafone.station/main.cgi?page=about.html
Accept-Language: it-IT
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Length: 230
Host: vodafone.station
Connection: Keep-Alive
Pragma: no-cache
Cookie: wbm_cookie_session_id=5D2D2E84416DB43D

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><DMCookie>70972421</DMCookie></soapenv:Header><soapenv:Body><cwmp:LoggedUser xmlns=""></cwmp:LoggedUser></soapenv:Body></soapenv:Envelope>

这是我的应用程序使用RestSharp发布的RAW请求:

POST http://vodafone.station/data_model.cgi HTTP/1.1
Accept: application/xml, text/xml, */*; q=0.01
SOAPAction: cwmp: LoggedUser
Pragma: no-cache
X-Requested-With: XMLHttpRequest
Referer: http://vodafone.station/main.cgi?page=index.html
Accept-Language: it-IT
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
Content-Type: text/xml
Host: vodafone.station
Cookie: wbm_cookie_session_id=29374DCA6F4B4B8B
Content-Length: 230
Accept-Encoding: gzip, deflate

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><DMCookie>657122696</DMCookie></soapenv:Header><soapenv:Body><cwmp:LoggedUser xmlns="></cwmp:LoggedUser></soapenv:Body></soapenv:Envelope>

我相信这两个请求基本相同,但第二个请求失败。 这里唯一的区别似乎是标题顺序,用户代理和缺少的方法标题。

如何更改RestSharp中标题的顺序?

更新

我尝试从其他浏览器中嗅探原始请求以将其用作基础:

POST http://vodafone.station/data_model.cgi HTTP/1.1
Host: vodafone.station
Connection: keep-alive
Content-Length: 232
Origin: http://vodafone.station
Method: POST
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36 Vivaldi/1.0.418.3
Content-Type: text/xml; charset="UTF-8"
Accept: application/xml, text/xml, */*; q=0.01
X-Requested-With: XMLHttpRequest
SOAPServer: 
SOAPAction: cwmp:LoggedUser
DNT: 1
Referer: http://vodafone.station/main.cgi?page=about.html
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: wbm_cookie_session_id=1FD0F3015AA2D293

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><DMCookie>1795351746</DMCookie></soapenv:Header><soapenv:Body><cwmp:LoggedUser xmlns=""></cwmp:LoggedUser></soapenv:Body></soapenv:Envelope>

这次我使用System.Net.HttpWebRequest类来重新创建请求:它可以工作,尽管我的最终目标是使用RestSharp。

所以,这有效:

POST http://vodafone.station/data_model.cgi HTTP/1.1
Origin: http://vodafone.station
Method: POST
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36 Vivaldi/1.0.418.3
Content-Type: text/xml; charset="UTF-8"
Accept: application/xml, text/xml, */*; q=0.01
X-Requested-With: XMLHttpRequest
SOAPServer: 
SOAPAction: cwmp:LoggedUser
DNT: 1
Referer: http://vodafone.station/main.cgi?page=about.html
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: wbm_cookie_session_id=04B71CB985477F19
Host: vodafone.station
Content-Length: 228

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><DMCookie>155497</DMCookie></soapenv:Header><soapenv:Body><cwmp:LoggedUser xmlns=""></cwmp:LoggedUser></soapenv:Body></soapenv:Envelope>

更新2

我尝试使用RestSharp发出上述请求:

所以,这是代码:

Function GetLoggedUser() As RestSharp.RestResponse

        Dim T As New TokenAndCookieGetter

        Dim xaction As String = "LoggedUser"
        Dim xbody As String = Envelope(T.DM_COOKIE, xaction)

        Dim req = New RestSharp.RestRequest("/data_model.cgi", RestSharp.Method.POST)

        req.AddHeader("Origin", "http://vodafone.station")
        req.AddHeader("Method", "POST")
        req.AddHeader("Accept", "application/xml, text/xml, */*; q=0.01")
        req.AddHeader("X-Requested-With", "XMLHttpRequest")
        req.AddHeader("SOAPServer", "")
        req.AddHeader("SOAPAction", "cwmp:" + xaction)
        req.AddHeader("DNT", "1")
        req.AddHeader("Referer", "http://vodafone.station/main.cgi?page=about.html")
        req.AddHeader("Accept-Encoding", "gzip, deflate")
        req.AddHeader("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4")
        'Cookie is already set in T.CLIENT, by assigning a CookieContainer.
        req.AddHeader("Host", "vodafone.station")

        req.AddParameter("text/xml; charset=""UTF-8""", xbody, RestSharp.ParameterType.RequestBody)

        Return T.CLIENT.Execute(req)

    End Function

这是相应的请求,但是很可能失败了:

POST http://vodafone.station/data_model.cgi HTTP/1.1
Origin: http://vodafone.station
Method: POST
Accept: application/xml, text/xml, */*; q=0.01
X-Requested-With: XMLHttpRequest
SOAPServer: 
SOAPAction: cwmp:LoggedUser
DNT: 1
Referer: http://vodafone.station/main.cgi?page=about.html
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36 Vivaldi/1.0.418.3
Content-Type: text/xml; charset="UTF-8"
Host: vodafone.station
Cookie: wbm_cookie_session_id=3FD15C2D16783BDD
Content-Length: 231

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><DMCookie>1534303835</DMCookie></soapenv:Header><soapenv:Body><cwmp:LoggedUser xmlns="></cwmp:LoggedUser></soapenv:Body></soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

更新3 我解决了这个问题。

所以我终于找到了问题。这不是任何标题。

这是一个糟糕的角色逃脱的情况。对我感到羞耻。

Dim envelope As String = String.Format("<cwmp:{0} xmlns="">{1}</cwmp:{0}>", action, body)

我没有转义双引号,因此请求总是会因为错误的XML(xmlns="属性)而导致500错误。

这是最后的工作要求:

POST http://vodafone.station/data_model.cgi HTTP/1.1
Origin: http://vodafone.station
Method: POST
Accept: application/xml, text/xml, */*; q=0.01
X-Requested-With: XMLHttpRequest
SOAPServer: 
SOAPAction: cwmp:LoggedUser
DNT: 1
Referer: http://vodafone.station/main.cgi?page=about.html
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36 Vivaldi/1.0.418.3
Content-Type: text/xml; charset="UTF-8"
Host: vodafone.station
Cookie: wbm_cookie_session_id=44D94CDF0F3ECD3C
Content-Length: 232

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><DMCookie>1554023074</DMCookie></soapenv:Header><soapenv:Body><cwmp:LoggedUser xmlns=""></cwmp:LoggedUser></soapenv:Body></soapenv:Envelope>