Solved

HttpWebRequest POST not redirecting

Posted on 2013-06-15
3
977 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
  • 2
3 Comments
 
LVL 74

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now