VB> HttpWebRequest with POST Method

I am invoking VB code to fetch JIRA cloud API and able to get positive results with GET request but POST request are creating issue. Proxy authentication error is coming.

Dim fr As System.Net.HttpWebRequest

Dim targetURI As New Uri(strURL)

Dim byteData() As Byte

Dim postStream As Stream

fr = DirectCast(HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)

Dim webProxy As IWebProxy = New WebProxy(strIpAddressAndPort)

webProxy.Credentials = New NetworkCredential(strProxyUsername, strProxyPassword)

fr.Proxy = webProxy

fr.Headers.Add(“Authorization”, jwtOutput)

fr.Headers.Add(“zapiAccessKey”, “MWExM2QyZTYtYzBlMi0zODI5LWIzMjUtZWNjZDJjM”)

fr.Method= “POST”

byteData = System.Text.Encoding.UTF8.GetBytes(NewContent.ToString())

fr.ContentType = “application/json”

fr.ContentLength = byteData.Length

MsgBox(byteData.Length)

postStream = fr.GetRequestStream()

postStream.Write(byteData, 0, byteData.Length)

Dim res As HttpWebResponse = DirectCast(fr.GetResponse(), System.Net.HttpWebResponse)

webRes = res

Dim html As String = New StreamReader(res.GetResponseStream()).ReadToEnd()

strHtml = html

*RemoteException wrapping System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.  ---> RemoteException wrapping System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required. *
*   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)   at System.Net.HttpWebRequest.GetRequestStream()   at UiPathCodeRunner_cbf3be3dabfa41f29ade6daba62745e1.Run(String strURL, String strIPAddressAndPort, String strProxyUsername, String strProxyPassword, HttpWebResponse& webRes, String& strHtml, String authenticationValue, String jwtOutput, String NewContent)*

@pushkar.singh

Can you try setting the default webprozy rather for only this request

WebRequest.DefaultWebProxy = proxy;

Cheers

Added fr.DefaultWebProxy = webProxy line after fr.Proxy = webProxy but same results.

@pushkar.singh

Ideally if get works this also should …

One thing we can try is routing the bot also through proxy server

Cheers

Code part didn’t work at my end… looking for any other way… i don’t know why it is throwing Proxy Authentication Required at System.Net.HttpWebRequest.GetRequestStream only

@pushkar.singh

Because that is where you are ideally posting the data to api request till then you are only building it

And the server requires proxy and for some reason the code is not routing to proxy server provided

Cheers

Yes, I understood but I am using same proxy details for GET request and things are working fine. Only POST request at line postStream = fr.GetRequestStream() throwing an error.

Proxy issue got fixed by using different credentials. When i execute query again, it is throwing (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse(). Any help?

Data being used> “{‘cycleId’:‘d932bc1f-f4e5-487f-8285-6b030704’,‘issueId’:‘719361’,‘projectId’:‘3984’,‘versionId’:‘122207’,‘accountId’:‘6228e89bc88f100068325da0’}”

@pushkar.singh

I guess you need to use double quotes right instead of single

cheers

No single quote. I checked it.

You were right, man. Thanks

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.