2013-01-01 43 views
2

我有這個PHP文件:發送POST值,並得到響應使用的HttpWebRequest

<?php 

if (isset($_POST['message'])) 
{ 
    $msg = $_POST['message']; 

    if($msg == "age") 
     echo "I am 18"; 
    else if($msg == "name") 
     echo "my name is Jonathan"; 
    else 
     echo "I do not know"; 

} 

?> 

我想打一個HttpWebRequest的在VB.NET這樣的,如果我從一個TextBox發送值「時代」: enter image description here
爲了得到這個以下的msgbox:
enter image description here

我想是這樣的:

Public Sub SendPostData(ByVal site As String, ByVal message As String) 

    Dim request As WebRequest 
    request = WebRequest.Create(site) 
    Dim response As WebResponse 
    Dim postData As String = "message=" + message 
    Dim data As Byte() = Encoding.UTF8.GetBytes(postData) 


    request.Method = "POST" 
    request.ContentType = "application/x-www-form-urlencoded" 
    request.ContentLength = data.Length 

    Dim stream As Stream = request.GetRequestStream() 
    stream.Write(data, 0, data.Length) 
    stream.Close() 

    response = request.GetResponse() 
    Dim sr As New StreamReader(response.GetResponseStream()) 


    MsgBox(sr.ReadToEnd) 


End Sub 


,我得到這個:

enter image description here


任何想法,爲什麼我收到,而不是在PHP文件3個可能的消息嗎? (「我18歲」,「我的名字是喬納森」,或者「我不知道」)? 請注意,我通常從一個html表單測試php,它工作,所以我想知道爲什麼它不是從程序工作。

+0

你能告訴哪個網站嗎? – theGD

+0

您是否嘗試過使用提琴手來查看請求?也許有些東西沒有被正確發送。 – Wade73

+0

theGD,我想你不明白我的問題 – Alex

回答

1

你在說什麼你得到是因爲你說,FORM內容是「X WWW的形式,進行了urlencoded」格式和您使用POST方法和一個假設的原因cookie中的任何內容(cookie數據)都是您通過的信息,但在發送前已由服務器編碼。

我不知道如何,但你應該能夠解碼它。 或者,您可以使用GET而不是POST,並且如果只有TEXT和沒有特殊符號或實際需要編碼的字符,那麼也許您可以使用不同的CONTENT-TYPE來正確標識您的內容類型正在發送...

在W3.org的下一頁是很好的閱讀,並解釋了各種零碎之間的差異以及它們如何與服務器之間以及它們之間的交互。

形式 ( http://www.w3.org/TR/html401/interact/forms.html

形式如何處理( http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3

1

你有沒有考慮過這樣做?

Option Strict On 
Public Class Form1 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     Dim Action As String = "http://www.mywebsite.com/myaction.php" 
     PostMessage("submit", "Paul", Action) 
    End Sub 
    Sub PostMessage(ByVal Type As String, ByVal Value As String, ByVal Action As String) 
     Dim TmpFile As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\post.html" 
     Using WB As New WebBrowser 
      Dim HTML As String = _ 
       "<html><body>" & 
       "<form method=""post"" action=""" & Action & """>" & 
       "<input type=""" & Type & """ value=""" & Value & """ />" & 
       "</form>" & 
       "</body></html>" 
      My.Computer.FileSystem.WriteAllText(TmpFile, HTML, False) 
      WB.Navigate(TmpFile) 
      Do : Application.DoEvents() 
      Loop Until WB.ReadyState = WebBrowserReadyState.Complete 
      WB.Document.GetElementById(Type).InvokeMember(Type) 
      Do : Application.DoEvents() 
      Loop Until WB.ReadyState = WebBrowserReadyState.Complete 
     End Using 
     My.Computer.FileSystem.DeleteFile(TmpFile) 
    End Sub 
End Class 
+0

使用WebBrowser有點沉重...所以當然我會堅持使用我的代碼,使用Http請求,除了我的所有Web服務器。問題是我想知道如何從服務器端解決這樣的問題,而不是從我這邊解決,因爲我的代碼沒有問題。我提醒你,它可以在其他網絡服務器上正常工作!我們是否需要在php.ini中編輯某些內容?我不知道..只是想知道.. – Alex

+0

爲什麼甚至使用郵政信息或PHP呢?你爲什麼不使用sql連接或什麼的。 –

1

我的經驗.NET軟件,並知道PHP也

決不用戶HttpWebRequest和httpwebresponse,「怎麼都是已過期

這裏是鏈接檢查正確 http://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx

它採用WebRequestWebResponse,添加引用System.NetSystem.Web在你的代碼

和目標框架必須是4.0,4。5或更高,但不能選擇包含客戶端配置文件的名稱

2

我已經使用WebClient解決了類似的問題,我需要在HTTP POST中使用參數。對於你的情況,像這樣的簡化解決方案可以幫助你充分利用你的代碼;假設你有一個文本框來捕捉年齡,你可以使用這段代碼作爲變量傳遞給url;

Using client As New WebClient 
     Dim postdata = client.UploadString(url, "age") 
     MsgBox(postdata) 
End Using 
0

只是改變這一行:

昏暗POSTDATA作爲字符串= 「消息=」 +消息

到:

昏暗POSTDATA作爲字符串= 「消息=」 &消息

使用「&」而不是「+」

Regards ...