我必须从以 .aspx 结尾的 Web 服务发送并获得响应。我使用了 HTTP post 方法。这是最好的方法吗?如何等待网络服务响应?要发送和获取响应,我使用以下代码。但我无法测试它,因为我没有网络服务。
WebRequest _request = WebRequest.Create("https://somewebsite.com/webservices/XMLInbound.aspx");
_request.Method = "POST";
XElement xele = new XElement("RAFeed");//Substitute here with XML File that is created with all the parameters
byte[] bytearray = Encoding.UTF8.GetBytes(xele.ToString());
_request.ContentType = "application/x-www-form-urlencoded";
_request.ContentLength = bytearray.Length;
_request.Timeout = 5000;// Here i wait for the response is the correct way ?
//Gets the request as a stream
Stream datastream = _request.GetRequestStream();
datastream.Write(bytearray, 0, bytearray.Length);
datastream.Close();
//Gets Response
WebResponse response = _request.GetResponse();
//// Display the status.
//Debug.WriteLine(((HttpWebResponse)response).StatusDescription);
datastream = response.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
string responseFromServer = reader.ReadToEnd();//Response is in this string
reader.Close();
datastream.Close();
response.Close();
var Rdoc = new XmlDocument();
Rdoc.LoadXml(responseFromServer);