2014-09-02 60 views
0

我只需要將來自C#桌面應用程序的xml數據發送到ASP.Net網頁。我的C#代碼看起來像這樣。C#將數據發佈到ASP .Net應用程序

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; 
} 

我的ASP .NET代碼看起來像這樣

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

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

labelsam是網絡上一個標籤page.But我labelsam.Is得到沒什麼反正有檢查數據是否received.Also什麼代碼錯誤?

+0

你回覆什麼回覆?此外,你應該看看利用'IDisposable'。利用'使用'。 – 2014-09-02 07:55:09

+0

如何檢查響應? – sameedhayat 2014-09-02 08:14:57

+0

呃,調試它?將它轉儲到控制檯,一個Windows窗體?無論你真的喜歡什麼。 – 2014-09-02 09:06:05

回答

1

您必須指定post方法的內容長度。

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(); 
相關問題