【问题标题】:Remote post to webpage from windows service从 Windows 服务远程发布到网页
【发布时间】:2013-04-30 06:11:42
【问题描述】:

注意:这个问题已经按照最初的要求进行了编辑

我正在开发一个 Windows 服务,该服务每天都会查询 Web 服务以获取数据,将其与当前存在的数据进行比较,然后将其发布到客户端的网页。

远程帖子的代码执行良好,但客户端网页上从未发生更新...

Imports Microsoft.VisualBasic
Imports System.Collections.Specialized

Public Class RemotePost

    Public Property Inputs As New NameValueCollection()
    Public Property URL As String
    Public Property Method As String = "post"
    Public Property FormName As String = "_xclick"

    Public Sub Add(ByVal name As String, ByVal value As String)
        _Inputs.Add(name, value)
    End Sub

    Public Sub Post()
        Dim client as New WebClient()
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
        Dim responseArray = client.UploadValues(URL, Method, Inputs)
    End Sub
End Class

我相信这是因为上面代码生成的帖子与表单本身手动发布的帖子不同。

以下是上述代码生成的帖子示例:

POST http://www.thisdomain.com/add-event/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: thisdomain.com
Content-Length: 116
Expect: 100-continue
Connection: Keep-Alive

no_location=0&event_name=Cycle-Fix+MTB+Tour+-+3+Days+ABC&event_start_date=2013%2f04%2f06&location_state=Eastern+Cape

以下是页面本身生成的帖子示例:

POST http://www.thisdomain.com/add-event/ HTTP/1.1
Host: thisdomain.com
Connection: keep-alive
Content-Length: 2844
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Origin: http://thisdomain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary8u14QB8zBLGQeIwu
Referer: http://thisdomain.com/add-event/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: UTF-8,*;q=0.5
Cookie: wp-settings-1=m5%3Do%26editor%3Dtinymce%26libraryContent%3Dbrowse%26align%3Dleft%26urlbutton%3Dnone%26imgsize%3Dfull%26hidetb%3D1%26widgets_access%3Doff; wp-settings-time-1=1367827995; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_8d8e9e110894b9cf877af3233b3a007b=admin%7C1368089227%7C69a33748ee5bbf638a315143aba81313; devicePixelRatio=1

------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="event_name"

test event
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="event_start_date"

2013-05-07
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="event_start_time"

01:00 AM
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="event_end_time"

02:15 AM
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="recurrence_freq"

daily
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="recurrence_interval"


------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="recurrence_byweekno"

1
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="recurrence_byday"

0
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="recurrence_days"

0
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="location_latitude"

38.0333333
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="location_longitude"

-117.23333330000003    

------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="location_name"

test
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="location_address"

test
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="location_town"

test
------WebKitFormBoundary8u14QB8zBLGQeIwu
Content-Disposition: form-data; name="location_state"

Georgia

显然,表单的帖子比我的帖子要多得多。不幸的是,我恐怕没有全部了解。

我对这些差异有以下担忧:

  • 我的(显然)查询字符串数据没有被读取,因为它没有被正确发送($_GET 而不是 $_POST)
  • PHP 中可能存在验证代码,该代码保存了这篇帖子之后的数据,这在某处阻塞了它

谁能解释一下这里到底有什么区别以及我如何根据我的要求模拟表单的帖子?

【问题讨论】:

    标签: php asp.net vb.net windows-services httpresponse


    【解决方案1】:

    你这样做的方式只是打印 html。它不发布。

    看看这个问题。

    POST to webpage in vb.net (win forms, desktop, not ASP.net)

    通过使用 WebClient 类,您可以将所需的信息发送到网站。 帖子数据的格式为:

    For i As Integer = 0 To Inputs.Keys.Count - 1
        if postData <> "" Then postData &= "&"
    
        postData &= Inputs.Keys(i) & "=" & Inputs(Inputs.Keys(i))
    Next
    

    【讨论】:

    • 好的,所以这正在工作......有点。事实证明,我要发布的表单需要使用表单数据来传递查询字符串数据。对交换有什么建议吗?
    • 发布数据使用 postData 变量(第三个参数)传递。查询字符串数据在 url(第一个参数)的末尾传递。 example.com/?data1=value1&data2=value2
    • 是的,我可以看到,但是出了点问题。它发布到页面,但从那里它没有被使用。我在 Fiddler 中看到的来自表单本身的请求数据的格式似乎与远程帖子发送的格式不同
    • 请再次查看问题。我已经更新它以说明我当前的问题
    • @Ortund 表单必须以“multipart/form-data”的形式发送数据。我不知道这能帮上什么忙……w3.org/TR/html401/interact/forms.html#h-17.13.4.2
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多