[英]Post parameters during asmx web service call
I am trying to make a web service call to following address: http://www.webservicex.net/CurrencyConvertor.asmx?op=ConversionRate 我正在尝试对以下地址进行Web服务调用: http : //www.webservicex.net/CurrencyConvertor.asmx?op=ConversionRate
However, I get WebException : Protocol Error
. 但是,我得到了
WebException : Protocol Error
。 So, I need some help to find where I went wrong. 因此,我需要一些帮助来查找我哪里出错了。 Thanks.
谢谢。
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);
}
}
I guess you are getting protocol exception because service is SOAP based. 我猜您正在收到协议异常,因为服务是基于SOAP的。 It would be easier to add service reference to that service from
http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
. 从
http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
向该服务添加服务引用会更容易。 And then you could use that service as following: 然后,您可以按以下方式使用该服务:
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();
}
[Edit] Sorry for misunderstanding. [编辑]抱歉造成误会。 Here is how to call SOAP services without adding service reference:
以下是在不添加服务引用的情况下调用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;
}
As you see, you should use xml for sending request. 如您所见,您应该使用xml来发送请求。 I added comments where they are necessary.
我在必要的地方添加了评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.