2013-03-26 58 views
0

我想訪問一個頁面,我需要登錄到網站並傳遞一個參數列表。我似乎能夠登錄到網站(如果我更改登錄詳細信息,我收到401未經授權的錯誤),但然後我得到一個400錯誤的請求錯誤。代碼有點散列在一起,所以我知道一些錯誤,但不知道在哪裏。使用ASP.net傳遞參數到外部網站

編輯的代碼

 Public Sub TestConn() 


    Dim customeremailaddress As String = HttpUtility.UrlEncode("[email protected]") 
    Dim customername As String = HttpUtility.UrlEncode("Ryan") 
    Dim referenceid As String = HttpUtility.UrlEncode("ordertest123") 
    Dim languagecode As String = HttpUtility.UrlEncode("1043") 
    Dim expirydays As String = HttpUtility.UrlEncode("30") 

    Dim UserName As String = "testusername" 
    Dim password As String = "testpassword" 
    Dim siteCredentials As New NetworkCredential(UserName, password) 

    Dim URLAuth As String = "http://service.someurl.com/process.xml" 
    Dim postString As String = String.Format("customeremailaddress={0}&customername={1}&referenceid={2}&languagecode={3}&expirydays={4}", customeremailaddress, customername, referenceid, languagecode, expirydays) 

    Dim postBytes As Byte() = Encoding.UTF8.GetBytes(postString) 

    Const contentType As String = "application/x-www-form-urlencoded" 
    System.Net.ServicePointManager.Expect100Continue = False 

    Dim cookies As New CookieContainer() 
    Dim webRequest__1 As HttpWebRequest = TryCast(WebRequest.Create(URLAuth), HttpWebRequest) 
    webRequest__1.Method = "POST" 
    webRequest__1.ContentType = contentType 
    webRequest__1.CookieContainer = cookies 
    webRequest__1.ContentLength = postBytes.Length 
    webRequest__1.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1" 
    webRequest__1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 
    webRequest__1.Referer = "http://service.someurl.com/process.xml" 
    webRequest__1.Credentials = siteCredentials 

    Try 
     Dim requestStream As Stream = webRequest__1.GetRequestStream() 
     requestStream.Write(postBytes, 0, postBytes.Length) 
     Dim responseReader As New StreamReader(webRequest__1.GetResponse().GetResponseStream()) 
     Dim responseData As String = responseReader.ReadToEnd() 
     responseReader.Close() 
     webRequest__1.GetResponse().Close() 

    Catch ex As Exception 
     Lbl_ConnTest_error.Text = ex.Message 
    End Try 

End Sub 
+0

Id通過使用Fiddler2 http://www.fiddler2.com/fiddler2/開始查看請求/響應詳細信息,並查看是否指向正確的方向。 – bUKaneer 2013-03-26 09:36:16

+0

另外看看這個例子http://msdn.microsoft.com/en-gb/library/debx8sh9.aspx - 它可能更容易閱讀 - 也作爲其發佈數據不要忘記正確格式postData即url編碼值部分和創建鍵值對,即變量1 =某些%20值和變量2 =另一個%20值 – bUKaneer 2013-03-26 09:41:29

+0

我認爲這是我可能會得到錯誤的請求,因爲我不知道實際傳遞參數列表的語法是什麼。我猜 - Dim postString As String = String.Format(「customeremailaddress = {0}&customername = {1}&referenceid = {2}&languagecode = {3}&expirydays = {4}」,customeremailaddress,customername,referenceid,languagecode,expirydays ) - 但我確定這看起來不正確? – Ryan 2013-03-26 09:57:17

回答

1

您需要發送的postString不是字符串本身的字節:

Dim postBytes As Byte() = Encoding.UTF8.GetBytes(postString) 
... 
webRequest__1.ContentLength = postBytes.Length 
... 
Dim requestStream As Stream = webRequest__1.GetRequestStream() 
requestStream.Write(postBytes, 0, postBytes.Length) 

更多信息here

+0

我已經做出了這些改變,但現在得到(400)壞請求就行了 - Dim responseReader As New StreamReader(webRequest__1.GetResponse()。GetResponseStream()) - 接近結束。 – Ryan 2013-03-26 09:50:24

+0

我已經整理了一些代碼,現在正在獲取:要寫入流的字節超過指定的Content-Length字節大小。有關UTF8編碼的事情? – Ryan 2013-03-27 10:27:21

+0

檢查當你設置'ContentLength'時,你指定'postBytes.Length'而不是'postString.Length'。還要檢查當你寫入流時你指定了postBytes.Length屬性。 – 2013-03-27 10:32:33