嗨,大家好我是新手編程和嘗試學習Web服務,請幫助我解決這個問題!爲什麼可選的xml參數在調用Web服務時不起作用?
我試圖使用httpWebRequest發佈肥皂xml到http://www.webservicex.net/globalweather.asmx使用以下與Visual Studio的XML。
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body>
<GetWeather xmlns='http://www.webserviceX.NET'>
<CountryName>Canada</CountryName>
<CityName></CityName>
</GetWeather>
</soap:Body>
</soap:Envelope>
這裏的問題是,如果我離開CITYNAME空白,它返回的數據沒有發現,但是當我用肥皂UI發送相同的XML,可以返回正確的氣象信息,因爲WSDL http://www.webservicex.net/globalweather.asmx?WSDL指出CITYNAME是可選。
希望如果有人能告訴我,我怎麼能得到這個工作。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace Assignment1.Tests
{
public partial class task2async : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//build url
UriBuilder _url = new UriBuilder();
_url.Scheme = "http";
_url.Host = "www.webservicex.net";
_url.Path = "globalweather.asmx";
string _action = "http://www.webserviceX.NET/GetWeather";
//creating a request
HttpWebRequest req=(HttpWebRequest)CreateRequest(_url.ToString(), _action);
//being async request stream
try
{
req.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), req);
}
catch (Exception ex)
{
Debug.WriteLine("Something wrong at asyncallback");
}
}
public static HttpWebRequest CreateRequest (string url, string action)
{
try {
//creating httpwebrequest object
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("SOAPAction", action);
request.ContentType = "text/xml;charset=\"UTF-8\"";
request.KeepAlive = true;
request.Method = "POST";
return request;
}
catch(Exception e)
{
return null;
}
}
public static XmlDocument createSoapEnvelop()
{
try
{
XmlDocument soapEvelope = new XmlDocument();
soapEvelope.LoadXml("<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><GetWeather xmlns='http://www.webserviceX.NET'><CountryName>Canada</CountryName><CityName></CityName></GetWeather></soap:Body></soap:Envelope>");
return soapEvelope;
}
catch (Exception e)
{
return null;
}
}
// using makes sure unused resources are released as soon
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest webRequest = (HttpWebRequest)callbackResult.AsyncState;
XmlDocument soapevelop = createSoapEnvelop();
Stream postStream = webRequest.EndGetRequestStream(callbackResult);
soapevelop.Save(postStream);
webRequest.BeginGetResponse(new AsyncCallback(GetResponseStreamCallback), webRequest);
}
void GetResponseStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
string result = httpWebStreamReader.ReadToEnd();
Debug.WriteLine(result);
}
}
}
}
爲什麼不只是使用服務引用?它爲你做了所有這些努力 –