Call Slack API in vb.NET with a proxy

864 views Asked by At

Trying to call a Slack API through an app in vb.NET behind a proxy. However, I don't have an expertise in .NET, so it's somehow out of my league.

This is the part of the code:

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

Then call it like that:

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

Without the proxy, it does work. With the proxy, I have the following error:

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...

If I try to make an HTTP post in postman app and use the above proxy, it is working. I guess the problem should be on the vb.net code.

1

There are 1 answers

2
Fraser On

Could be mistaken but I am pretty sure the address for your proxy is wrong. You need to pass the port separately as Int to the address URL. i.e.

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

Where thisismyport is a Int value of the port number you need.

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

Based on your comment I would Imagine that the URL you are specifying for your proxy is either;

a) incorrect, or...

b) can't be resolved via DNS

To fix, ensure that the URL "http://thisismyproxy" is 100% correct - also try entering the ip address of the proxy server rather than the domain name. e.g.

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

Does that resolve the issue? If not can you reach (ping) etc the proxy server from the machine - that is - can you resolve thisismyproxy at all?