0

All I want is to send xml data from C# desktop application to ASP.Net webpage. My C# code look like this.

public string SendRequest()
{   string data = "<?xml version="1.0"?><author>Gambardella, Matthew</author>";
    string _result;
    Uri uri = new Uri("http://localhost:62511/Default");
    var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST";
    request.ContentType = "text/xml";
    var writer = new StreamWriter(request.GetRequestStream());
    writer.Write(data);
    writer.Close();

    var response = (HttpWebResponse)request.GetResponse();

    var streamResponse = response.GetResponseStream();
    var streamRead = new StreamReader(streamResponse);
    Console.Write(response.StatusCode);
    _result = streamRead.ReadToEnd().Trim();
    streamRead.Close();
    streamResponse.Close();
    response.Close();
    return _result;
}

My ASP .Net Code looks like this

protected void Page_Load(object sender, EventArgs e)
{
    using (var reader = new StreamReader(Request.InputStream))
    {
        string xml = reader.ReadToEnd();

        labelsam.Text = xml;
    }
    ....
}

labelsam is a label on web page.But I get nothin in labelsam.Is there anyway to check whether the data is received.Also whats wrong with code?

5
  • What response/s do you get back? Also, you should look at making use of IDisposable. Make use of using. Commented Sep 2, 2014 at 7:55
  • how to check the response? Commented Sep 2, 2014 at 8:14
  • Erm, debug it? Dump it out to the console, a windows form? Whatever's your preference really. Commented Sep 2, 2014 at 9:06
  • Thanks.I see the data on console but not on webpage.Can you please tell me whats the problem? Commented Sep 2, 2014 at 9:08
  • It's pretty clear that you're a beginner to C# and programming in general. I suggest that you start by doing some basic tutorials first before tackling something like this. When you're done, you'll have the tools and the knowhow to figure it out for yourself. Commented Sep 2, 2014 at 9:18

1 Answer 1

1

You have to specify content length for post method.

var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength=data.Length; //ugly, but at least so
var writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.