Solved

ASP.NET Receive HTTP POST

Posted on 2011-03-08
5
1,122 Views
Last Modified: 2013-11-27
Hi Experts I have the code attached, trying to receive  a POST from a 3rd party SMS provider. They say in PHP it looks something like
<?php
$sender = $_REQUEST['sender'];
$content = $_REQUEST['content'];
$inNumber = $_REQUEST['inNumber'];
$email = $_REQUEST['email'];
$credits = $_REQUEST['credits'];
?>
I need to translate this to ASP.NET and give them the URL they are supposed to send the POST to. would response.aspx be sufficient?
Imports System.IO
Imports System.Net

Partial Class response
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strDataToPost As String

        Dim myWebRequest As WebRequest
        Dim myRequestStream As Stream
        Dim myStreamWriter As StreamWriter

        Dim myWebResponse As WebResponse
        Dim myResponseStream As Stream
        Dim myStreamReader As StreamReader

        ' Create a new WebRequest which targets that page with which we want to interact.
        myWebRequest = WebRequest.Create("response.aspx")

        ' Set the method to "POST" and the content type so the server knows to expect form data in the body of the request.
        With myWebRequest
            .Method = "POST"
            .ContentType = "application/x-www-form-urlencoded"
        End With

        myResponseStream = myWebResponse.GetResponseStream()
        myStreamReader = New StreamReader(myResponseStream)


        Dim FILENAME As String = Server.MapPath("TextFile.txt")
        File.Delete(FILENAME)
        Dim objStreamWriter As StreamWriter
        objStreamWriter = File.AppendText(FILENAME)

        objStreamWriter.WriteLine(myStreamReader.ReadToEnd())
        objStreamWriter.Close()

        myStreamReader.Close()
        myResponseStream.Close()

        ' Close the WebResponse
        myWebResponse.Close()

    End Sub

Open in new window

0
Comment
Question by:takwirirar
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35068519
If they are POSTing to you then you just need to provide them with the URL of the page that will handle the request.
0
 
LVL 1

Author Comment

by:takwirirar
ID: 35068943
Thank you I have given the URL and done a test but nothing happens the way it should i.e. as per my example something is supposed to be wriiten to a text file
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35069016
They are sending information to you, but your code is geared up to send data to your own page rather than simply reading the data being sent to it.

You should be doing something more like:
Protected Sub Page_Load(sender As Object, e As EventArgs)

        Dim send As String = Request.Form("sender'")
        Dim content As String = Request.Form("content")
        Dim inNumber As String = Request.Form("inNumber")
        Dim email As String = Request.Form("email")
        Dim credits As String = Request.Form("credits")

        Dim FILENAME As String = Server.MapPath("TextFile.txt")
        File.Delete(FILENAME)
        Dim objStreamWriter As StreamWriter
        objStreamWriter = File.AppendText(FILENAME)

        Dim line As String = String.Format("{0}, {1}, {2}, {3}, {4}", send, content, inNumber, email, credits)

        objStreamWriter.WriteLine(line)
        objStreamWriter.Close()

End Sub

Open in new window

0
 
LVL 1

Author Comment

by:takwirirar
ID: 35069086
Thanks Carl, I've uploaded your code with a few variations i.e. to send me an email instead.Nothing comes through. Find my code.
Imports System.IO
Imports System.Net
Imports System.Net.Mail

Partial Class response
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim send As String = Request.Form("sender'")
        Dim content As String = Request.Form("content")
        Dim inNumber As String = Request.Form("inNumber")
        Dim email As String = Request.Form("email")
        Dim credits As String = Request.Form("credits")

        Dim line As String = String.Format("{0}, {1}, {2}, {3}, {4}", send, content, inNumber, email, credits)

        'create the mail message
        Dim mail As New MailMessage()

        'set the addresses
        mail.From = New MailAddress("takwirira@gmail.com")
        mail.To.Add("takwirira@gmail.com")

        'set the content
        mail.Subject = "Incoming SMS"
        mail.Body = line

        'send the message
        Dim smtp As New SmtpClient("localhost") 'specify the mail server address
        smtp.Send(mail)
    End Sub
End Class

---------------------



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

<!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>
    
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 1

Author Closing Comment

by:takwirirar
ID: 35069095
Absolutely wonderful!!!! Thank you so much
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

920 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