Solved

HttpWebRequest POST not redirecting

Posted on 2013-06-15
3
989 Views
Last Modified: 2013-06-20
I have the code below,

        Dim url As String = "https://stageserv.interswitchng.com/test_paydirect/pay"

        Dim postData As New StringBuilder()

        'ETC for all Form Elements
        postData.Append("product_id=" & HttpUtility.UrlEncode(product_id.Text))
        postData.Append("&amount=" & HttpUtility.UrlEncode(amount.Text))
        postData.Append("&currency=" & HttpUtility.UrlEncode(currency.Text))
        postData.Append("&site_redirect_url=" & HttpUtility.UrlEncode(site_redirect_url.Text))
        postData.Append("&txn_ref=" & HttpUtility.UrlEncode(txn_ref.Text))
        postData.Append("&pay_item_id=" & HttpUtility.UrlEncode(pay_item_id.Text))
        postData.Append("&hash=" & HttpUtility.UrlEncode("148182038CB072F341FEDF2B9597B6CBF15A2EB01BD98CA6F890B0F60245281DA5AA970E141C2549C2DCA5626AD6E371920C4B72AA54AE98D48AFFA41C8E8798"))

        'generate and append hash
        'postData.Append("hash=" & HttpUtility.UrlEncode(EncryptSHA512Managed("764553644921011000000http://www.westernmall.co.uk/test/response.aspxFF8F097D1DE0EFC14DE92F65085DED46A796BDFA900AAD1BE0E86ECE8F5B234052843A0754FE27F07E63A3649FD8121329FBAB4F0D7924557C59122BCD4BF13F")))

        ' Now to Send Data.
        Dim writer As StreamWriter = Nothing

        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = postData.ToString().Length
        request.AllowAutoRedirect = True

        Try
            writer = New StreamWriter(request.GetRequestStream())
            writer.Write(postData.ToString())
        Finally
            If writer IsNot Nothing Then
                writer.Close()
            End If
        End Try

Open in new window


I am trying to mimic the HTML version below but the only difference is the code above does not redirect to the URL provided

<form name="form1" action="http://stageserv.interswitchng.com/test_paydirect/pay" method="post">
<input name="product_id" type="hidden" value="4492" />
<input name="amount" type="hidden" value="1000000" />
<input name="currency" type="hidden" value="566" />
<input name="site_redirect_url" type="hidden" value="http://www.westernmall.co.uk/test/response.aspx" />
<input name="txn_ref" type="hidden" value="7645536"/>
<input name="pay_item_id" type="hidden" value="101" />
<input name="hash" type="hidden" value="148182038CB072F341FEDF2B9597B6CBF15A2EB01BD98CA6F890B0F60245281DA5AA970E141C2549C2DCA5626AD6E371920C4B72AA54AE98D48AFFA41C8E8798" />
<input name="Button" type="Submit" value="Click Me to load the payment page" />
</form>

Open in new window


Any ideas?
0
Comment
Question by:takwirirar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 39249944
Of course not! An HttpWebRequest represents a single request. A redirect would be a subsequent request. You need to inspect the headers of the response to your first request and extract the Location header. Then use that value in a 2nd request to accomplish the redirect.
0
 
LVL 1

Accepted Solution

by:
takwirirar earned 0 total points
ID: 39249951
Thank you. This achieved the result

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnSearch" runat="server" PostBackUrl="https://stageserv.interswitchng.com/test_paydirect/pay" Text="Make Payment"/>
        
        <asp:HiddenField ID="product_id" runat="server" Value="4492" />
        <asp:HiddenField ID="amount" runat="server" Value="1000000" />
        <asp:HiddenField ID="currency" runat="server" Value="566" />
        <asp:HiddenField ID="site_redirect_url" runat="server" Value="http://www.westernmall.co.uk/test/response.aspx" />
        <asp:HiddenField ID="txn_ref" runat="server" Value="7645536" />
        <asp:HiddenField ID="pay_item_id" runat="server" Value="101" />
        <asp:HiddenField ID="hash" runat="server" Value="148182038CB072F341FEDF2B9597B6CBF15A2EB01BD98CA6F890B0F60245281DA5AA970E141C2549C2DCA5626AD6E371920C4B72AA54AE98D48AFFA41C8E8798" />
       
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 1

Author Closing Comment

by:takwirirar
ID: 39261857
Found out the answer after more Google searches.
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question