2013-12-09 69 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编码您的秘密令牌。

希望它有帮助。