从Windows服务远程发布到网页

时间:2013-05-06 13:14:59

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

注意:此问题已经过编辑,原来是

我正在开发一项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中可能存在验证代码,用于在此帖子之后保存数据

有谁可以解释这里的确切差异以及我如何根据我的要求模仿表格的帖子?

1 个答案:

答案 0 :(得分: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