0

我有一个来自第三方的 .jsp 页面,我需要从中获取信息。我的应用程序正在 MVC4 中开发。我将如何在我的应用程序中从这个 .jsp 文件中获取信息。

我尝试使用 webrequest,但内容不存在。

问候法特玛

4

2 回答 2

1

I think you have two options:

The first option is to do this on the client side using Ajax: (http://api.jquery.com/jQuery.ajax/

Code could look something like:

function CallOtherSite(otherSiteUrl) {  
$.ajax({
    url: otherSiteUrl,
    cache: false,
    success: function (html) {
     //This will be the html from the other site
    //parse the html/xml and do what you need with it.
    }
});

Because this is done with JavaScript on the client side you will most likely run into a problem with CORS. (http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx)

The other option and better option in my opinion is to do this on the server side. (Either in a controller or view with Razor) (It will be much easier in the contoller...)

try
{
    var request = (HttpWebRequest)WebRequest.Create(urlToOtherSite);
    request.Accept = "application/xml";
    request.Method = "GET";
    webResponse = (HttpWebResponse)request.GetResponse();
    sr = new StreamReader(webResponse.GetResponseStream());
    string responseText = sr.ReadToEnd();
}
catch(Exception ex)
{
}
finally
{
    if (sr != null) { sr.Close(); }
    if (webResponse != null) { webResponse.Close(); }
}

Then you can use the StreamReader to get the html/xml and do what you will with it.

Hope this helps...

于 2013-11-16T19:06:13.537 回答
1

JSP 页面必须由像 tomcat 这样的 Servlet-Container 实际呈现,因为包含的数据是动态的。完成后,您可以使用 .net 应用程序解析 HTML 输出。

这可能是唯一的方法。直接从jsp读取数据。

无论如何,我建议您找到另一种方法来检索数据,例如将 API 添加到 jsp 所属的 Java EE 应用程序。或访问现有的。

于 2013-11-07T11:58:09.117 回答