[英]Post parameters during asmx web service call
我正在嘗試對以下地址進行Web服務調用: http : //www.webservicex.net/CurrencyConvertor.asmx?op=ConversionRate
但是,我得到了WebException : Protocol Error
。 因此,我需要一些幫助來查找我哪里出錯了。 謝謝。
protected void Page_Load(object sender, EventArgs e)
{
try
{
string FromCurrency = "GBP";
string ToCurrency = "ALL";
string postString = string.Format("FromCurrency={0}&ToCurrency={1}", FromCurrency, ToCurrency);
const string contentType = "application/x-www-form-urlencoded";
System.Net.ServicePointManager.Expect100Continue = false;
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.webservicex.net/CurrencyConvertor.asmx?op=ConversionRate");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Method = "POST";
req.ContentLength = postString.Length;
req.Referer = "http://webservicex.net";
StreamWriter requestWriter = new StreamWriter(req.GetRequestStream());
requestWriter.Write(postString);
requestWriter.Close();
StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
req.GetResponse().Close();
}
catch (WebException ex)
{
Response.Write("\r\nWebException Raised. The following error occured : {0}" + ex.Status);
}
catch (Exception exc)
{
Response.Write("\nThe following Exception was raised : {0}" + exc.Message);
}
}
我猜您正在收到協議異常,因為服務是基於SOAP的。 從http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
向該服務添加服務引用會更容易。 然后,您可以按以下方式使用該服務:
static void Main(string[] args)
{
ServiceReference1.CurrencyConvertorSoapClient client = new ServiceReference1.CurrencyConvertorSoapClient("CurrencyConvertorSoap");
client.Open();
double conversionResult = client.ConversionRate(ServiceReference1.Currency.AED, ServiceReference1.Currency.ANG);
Console.WriteLine("{0} to {1} conversion rate is {2}", ServiceReference1.Currency.AED, ServiceReference1.Currency.ANG, conversionResult);
client.Close();
Console.Read();
}
[編輯]抱歉造成誤會。 以下是在不添加服務引用的情況下調用SOAP服務的方法:
static void Main(string[] args)
{
HttpWebRequest request = CreateWebRequest();
//Create xml document for soap envelope
XmlDocument soapEnvelopeXml = new XmlDocument();
//string variable for storing body of xml document
//with parameters for FromCurrency and ToCurrency
string soapEnvelope =
@"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Body xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<ConversionRate xmlns=""http://www.webserviceX.NET/"">
<FromCurrency>{0}</FromCurrency>
<ToCurrency>{1}</ToCurrency>
</ConversionRate>
</s:Body>
</s:Envelope>";
//add your desired currency types
soapEnvelopeXml.LoadXml(string.Format(soapEnvelope, "USD", "EUR"));
//add your xml to request
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
//call soap service
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd(); //read the xml result
//you can handle xml and get the conversion result, but here
//I'm outputting raw xml.
Console.WriteLine(soapResult);
}
}
Console.Read();
}
public static HttpWebRequest CreateWebRequest()
{
//create web request for calling soap service
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://www.webservicex.net/CurrencyConvertor.asmx");
//add soap header
webRequest.Headers.Add(@"SOAP:Action");
//content type should be text/xml
webRequest.ContentType = "text/xml; charset=\"utf-8\"";
//response also will be in xml, so request should accept it
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
如您所見,您應該使用xml來發送請求。 我在必要的地方添加了評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.