Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HttpWebRequest POST not redirecting

Posted on 2013-06-15
3
Medium Priority
?
994 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 2000 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 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