【问题标题】:Call Slack API in vb.NET with a proxy使用代理在 vb.NET 中调用 Slack API
【发布时间】:2018-07-22 09:33:14
【问题描述】:

试图通过代理后面的 vb.NET 中的应用调用 Slack API。但是,我没有 .NET 方面的专业知识,因此不知何故超出了我的范围。

这是代码的一部分:

Private Function GetResponseFromPostRequest(ByVal url As String, ByVal variables As String) As String
    Dim content As String

    Dim postData() As Byte = Encoding.GetEncoding("utf-8").GetBytes(variables)
    Dim req As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
    Dim proxyObject As New WebProxy("http://thisismyproxy:thisismyport")

    req.Proxy = proxyObject
    req.Method = "POST"

    req.ContentType = "application/json"
    req.ContentLength = postData.Length

    Dim postStream As Stream = req.GetRequestStream()
    postStream.Write(postData, 0, postData.Length)
    postStream.Close()

    Using res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)
        Using receiveStream As Stream = res.GetResponseStream()
            Dim readStream As New StreamReader(receiveStream, Encoding.GetEncoding("utf-8"))
            content = readStream.ReadToEnd()
        End Using
    End Using

    Return content

End Function

然后这样称呼它:

GetResponseFromPostRequest("https://hooks.slack.com/services/....",  "{'text':'" & slackTitle & "'}")

没有代理,它确实有效。使用代理,我有以下错误:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ...an api...

如果我尝试在邮递员应用程序中发布 HTTP 帖子并使用上述代理,它就可以工作。我想问题应该出在 vb.net 代码上。

【问题讨论】:

  • 也许用 WireShark 看看是怎么回事?

标签: vb.net proxy


【解决方案1】:

可能是错误的,但我很确定您的代理地址是错误的。您需要将端口作为 Int 单独传递给地址 URL。即

 Dim proxyObject As New WebProxy("http://thisismyproxy", thisismyport)

其中thisismyport 是您需要的端口号的 Int 值。

见:https://msdn.microsoft.com/en-us/library/xfsh37cx(v=vs.110).aspx

根据您的评论,我想您为代理指定的 URL 是;

a) 不正确,或者...

b) 无法通过 DNS 解析

要解决此问题,请确保 URL“http://thisismyproxy”100% 正确 - 同时尝试输入代理服务器的 IP 地址而不是域名。例如

Dim proxyObject As New WebProxy("http://192.168.x.x", 8080)

这能解决问题吗?如果不能,您可以从机器访问(ping)等代理服务器 - 也就是说 - 你能解决这个问题吗?

【讨论】:

  • 当我按照你所说的进行更改时,我收到此错误“无法解析远程名称:http”
  • 查看我的编辑,这听起来像是代理地址的 DNS 问题。
猜你喜欢
  • 1970-01-01
  • 2019-09-17
  • 2016-09-08
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多