如何使用JavaScript获取webservice XML
如何将Java脚本与HTTP Web服务结合使用,这是HTTP get请求:如何使用JavaScript获取webservice XML,java,xml,http,get,Java,Xml,Http,Get,如何将Java脚本与HTTP Web服务结合使用,这是HTTP get请求: 'GET /stockquote.asmx/GetQuote?symbol=string HTTP/1.1 Host: www.webservicex.net HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?&g
'GET /stockquote.asmx/GetQuote?symbol=string HTTP/1.1
Host: www.webservicex.net
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.webserviceX.NET/">string</string>'
您发现的代码不起作用的原因是它被包装在单引号中,并且依赖于名为的第三方库。如果您从页面链接到jQuery(并删除jQuery字符串周围的单引号),并使用正确的参数指向$.get方法中的正确URL,它可能会工作得很好。我用C完成了这项工作# 使用System.IO
public class GetData
{
public static string HTTP_GET(string Url, string Data)
{
string Out = String.Empty;
System.Net.WebRequest req = System.Net.WebRequest.Create(Url + (string.IsNullOrEmpty(Data) ? "" : "?" + Data));
try
{
System.Net.WebResponse resp = req.GetResponse();
using (System.IO.Stream stream = resp.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(stream))
{
Out = sr.ReadToEnd();
sr.Close();
}
}
}
catch (ArgumentException ex)
{
Out = string.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}",
}
catch (WebException ex)
{
Out = string.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message);
}
catch (Exception ex)
{
Out = string.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message);
}
return Out;
}
}
[System.Web.Services.WebMethod]
public static string getQuote()
{
XmlDocument quoteXML = new XmlDocument();
string strQuote = GetData.HTTP_GET("http://www.mywebservice/stockquote.asmx/GetQuote", "symbol=lloy.l");
return strQuote;
}'
谢谢adv12,我希望有一些更有建设性的东西。是的,我的代码中有一个JQUERY库引用,在stackoverflow上发布代码需要引号。如果你有任何有用的意见,我将不胜感激。
'Using System.Net;
public class GetData
{
public static string HTTP_GET(string Url, string Data)
{
string Out = String.Empty;
System.Net.WebRequest req = System.Net.WebRequest.Create(Url + (string.IsNullOrEmpty(Data) ? "" : "?" + Data));
try
{
System.Net.WebResponse resp = req.GetResponse();
using (System.IO.Stream stream = resp.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(stream))
{
Out = sr.ReadToEnd();
sr.Close();
}
}
}
catch (ArgumentException ex)
{
Out = string.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}",
}
catch (WebException ex)
{
Out = string.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message);
}
catch (Exception ex)
{
Out = string.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message);
}
return Out;
}
}
[System.Web.Services.WebMethod]
public static string getQuote()
{
XmlDocument quoteXML = new XmlDocument();
string strQuote = GetData.HTTP_GET("http://www.mywebservice/stockquote.asmx/GetQuote", "symbol=lloy.l");
return strQuote;
}'