2013-12-09 71 views
0

嗨我想在我的代碼背後(vb.net)在我的asp.net頁面與web服務進行通信時,當我撥打電話我得到401未經授權的錯誤,但是當我嘗試同一個電話,雖然簡單的HTML頁面的作品。問題調用webservice

這裏是我的簡單的HTML頁面

 <form method="post" action="https://mycallingwebpage.co.uk/login"> 
    <input type="submit"> 
    </form> 

能正常工作並返回我所期望的那樣。

但是在我的vb.net代碼中這是行不通的。

Public Function mylogin(ByVal ServiceProfile) As String 
    Dim xmldoc As New Xml.XmlDocument 
    Try 
     myWebClient = New WebClient 

     myWebClient.Headers.Add("Content-Type", "text/plain") 

     Dim bytRetData As Byte() = myWebClient.DownloadData("https://mycallingwebpage.co.uk/login" & ServiceProfile) 

    Catch ex As Exception 

    End Try 

End Function 

我現在已經取得了一定的前進腳步。

除了我上面的問題之外,我仍然無法撥打電話。我發現,如果我打電話給http url它的工作,但調用https url它不 - 這個ajax調用下面的工作對我來說 - 如此有效我需要做什麼將下面的代碼轉換成一個webclient在vb.net中調用? - 這需要爲https工作

$.ajax({ 
       type: "POST", 
       contentType: 'application/x-www-form-urlencoded', 
       dataType: "application/xml", 
       crossDomain: true, 
       data: {'login' : encodeURIComponent('user'),'password' : encodeURIComponent('password')} , 
       url: "https://myURL.com", 
       success: doThis, 
       error: doThat 
      }); 
+0

什麼是'ServiceProfile'? –

+1

你確定你沒有在vb代碼中隱藏異常嗎? –

+0

是的,擺脫那個try/catch塊,非常第一件事。 –

回答

0

您必須在訪問WS之前「自我授權」。您可能缺少授權標頭。對不起,我沒有太大的VB專家,但它應該是這樣的:

myWebClient.Headers.Add("Authorization", "Basic " + ADD_UR_SECRET_HERE) 

BTW,不要忘了將它添加到頭部之前編碼使用base64編碼您的祕密令牌。

希望它有幫助。