?
Solved

ASP.NET Receive HTTP POST

Posted on 2011-03-08
5
Medium Priority
?
1,146 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
[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
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

770 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