'Receive POST data on Web Service

I have a website which is supposed to receive data from a web form. I cannot access the code of the webform.

I can input the link where the data should be send on the web form and it sends me the data in the form of a string to my webpage. I'm supposed to receive it and store it in the database.

I made a web service with an empty public web method where I can receive the data. I've tried to implement several functions on my webpage which could access the data but I just can't seem to get it. I've tried HTTPListeners and HTTPWebRequests but I'm not sure I'm using it in the right way.

The code in the web service

public class WebService : System.Web.Services.WebService
{
    public WebService()
    {

    }
[WebMethod]
public string getData()
{
    return "Hello World";
}
}

The code in ASP.NET / Website

private const string URL = "http://localhost:80/WebService.asmx/getData/";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.Method = "GET";
        request.ContentType = "text/plain";

        WebResponse webResponse = (HttpWebResponse)request.GetResponse();
        Stream webStream = webResponse.GetResponseStream();
        StreamReader responseReader = new StreamReader(webStream);
        string response = responseReader.ReadToEnd();
        MessageBox.Show(response);                   //To display the received data
        // System.Diagnostics.Debug.WriteLine(response);
        responseReader.Close();

    }
}

I've searched everything and tried all possible solutions but the code just doesn't seem to work. I'd be grateful if someone could help out.



Solution 1:[1]

I would suggest looking at HttpHandler and more specifically the ProcessRequest method. You can grab the HttpContext in the method which should give you access to all the content that is being sent. If HttpHandlers are new, this might be able to help a bit: Introduction to HTTP Handlers

Edit:

Implement the IHttpHandler interface and create the ProcessRequest method. From the context, you can grab the request and inside the request is the form which has the formvariables as a NameValueCollection. More info is here (NameValueCollection)

public class SomeHandler : IHttpHandler
{
  public void ProcessRequest(HttpContext context)
  {
    var request = context.Request;
    NameValueCollection formVariables = request.Form;
    //do something to process the collection
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1