Solved

HttpWebRequest POST not redirecting

Posted on 2013-06-15
3
980 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scheduled IIS .Net2 AppPools recycle and SQL connection Hangs 33 113
Help with C#, MVC, razor. 6 34
ASP.net show message while code is running 3 17
ASP.NET MVC 2 31
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 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

8 Experts available now in Live!

Get 1:1 Help Now