Solved

ASP.NET Receive HTTP POST

Posted on 2011-03-08
5
1,117 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A few customers have recently asked my thoughts on Password Managers.  As Security is a big part of our industry I was initially very hesitant and sceptical about giving a program all of my secret passwords.  But as I was getting asked about them mo…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

762 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

16 Experts available now in Live!

Get 1:1 Help Now