Link to home
Create AccountLog in
Avatar of David C
David CFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

If they are POSTing to you then you just need to provide them with the URL of the page that will handle the request.
Avatar of David C

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of David C

ASKER

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

Avatar of David C

ASKER

Absolutely wonderful!!!! Thank you so much