• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1157
  • Last Modified:

ASP.NET Receive HTTP POST

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
takwirirar
Asked:
takwirirar
  • 3
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
takwirirarAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
takwirirarAuthor Commented:
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
 
takwirirarAuthor Commented:
Absolutely wonderful!!!! Thank you so much
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now