Solved

HttpWebRequest POST not redirecting

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

751 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