2016-02-17 67 views
1

我需要从经典的asp页面调用我的.NET(C#)web服务。我想通过创建一个调用asp页面的控制台应用程序来测试它。 这是我的ASP页:从控制台应用程序调用经典的asp页面

Dim strUserID 
Dim strUserName 
Dim strUserEmail 

strUserID = Request.Form("UserID") 
strUserName = Request.Form("UserName") 
strUserEmail = Request.Form("UserEMail") 

SET objSoapClient = Server.CreateObject("MSSOAP.SoapClient") 
objSoapClient.ClientProperty("ServerHTTPRequest") = True 

Call objSoapClient.mssoapinit("http://localhost:/MyWebService/Service1/" & _ 
            "MyWebService.asmx?WSDL", "MyWebService") 

strReturnValue = objSoapClient.SendData(strUserID, strUserName, strUserEmail) 
response.Write("Returned from service with return value: " & strReturnValue) 

现在我的控制台应用程序必须调用.asp页。 如何构建URL? 如果asp页面位于以下文件夹中:C:\ Folder1 \ OldPage.asp,我该如何构建URL?

这是我到目前为止有:

static void Main(string[] args) 
{ 
    WebClient client = new WebClient();  
    Uri aspPagingServiceUri = new Uri("http://localhost/Folder1/OldPage.asp?UserID=g39s24&UserName=Gloria [email protected]"); 
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(aspPagingServiceUri);  
    httpWebRequest.Method = "GET"; 
    var response = httpWebRequest.GetResponse(); 
    HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse(); 
    Stream resStream = resp.GetResponseStream(); 

    StreamReader reader = new StreamReader(resStream); 
    string strResponse = reader.ReadToEnd(); 
    Console.WriteLine(strResponse); 

    reader.Close(); 
} 

我得到的错误:“远程服务器返回错误:(503)服务器不可用。 '当它到达GetResponse函数。

我相信我的问题是与URL的创建。

谢谢。

UPDATE 我试图连接到Web服务器上的ASP文件。我收到(500)错误:“远程服务器返回错误:(500)内部服务器错误。” 该文件夹是:C:\的Inetpub/wwwroot文件/应用程序/服务/ ServiceNew.asp 这是我的控制台应用程序:

static void Main(string[] args) 
{ 
    try 
    { 
     WebClient client = new WebClient(); 
     Uri aspPagingServiceUri = new Uri("http://myserverName/Apps/Services/ServiceNew.asp?UserID=g39r345&UserName=John Smith&[email protected]&Subject=Test&MSG=Testing&ContactList=Sam Smith;"); 
     HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(aspPagingServiceUri); 
     httpWebRequest.Method = "GET"; 
     var response = httpWebRequest.GetResponse(); 
     HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse(); 
     Stream resStream = resp.GetResponseStream(); 

     StreamReader reader = new StreamReader(resStream); 
     string strResponse = reader.ReadToEnd(); 
     Console.WriteLine(strResponse); 

     reader.Close(); 
    } 
    catch (WebException wex) 
    { 
     Console.WriteLine("Web Exception: " + wex.Message); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine("General Exception: " + ex.Message); 
    } 
} 

错误发生var response = httpWebRequest.GetResponse();

我是否正确地创建网址是什么?
我只是跟随文件路径。

+0

我正在调用一个经典的ASP文件来运行。我不知道它是否有特殊的库,要添加的组件。 –

+0

道歉 - 我会删除。 – Paul

回答

2

您不能直接执行ASP代码。您需要设置可在本地网络中的另一台计算机上或外部执行经典ASP(即Internet信息服务(IIS))的Web服务器。

IIS是某些Windows发行版的一部分,您可能只需激活它即可。确保安装传统的ASP模块,因为现在缺省情况下未安装该模块。

警告:传统的ASP通常依赖于额外的COM组件。所以你可能需要安装更多才能使你的代码正常工作。

+0

谢谢。我将这个ASP文件从我们的一个应用程序正在调用的Web服务器中提取出来。所以,从理论上讲,我可以将该文件放在该Web服务器上,并使用我的控制台应用程序调用它。 –

+0

是的,ASP文件只能在Web服务器上运行。您可以将URL放在一起并在浏览器中进行测试,并在控制台应用程序中使用该URL。如果您的控制台应用程序只是为了调用,您也可以在Linux/Mac上的PowerShell或'curl'中使用'Invoke-WebRequest'(https://technet.microsoft.com/en-us/library/hh849901.aspx)一个Web服务。 – gpinkas

+0

控制台应用程序只是模拟将运行ASP文件的应用程序的驱动程序。我必须更改ASP文件来调用我使用C#.NET 4.0 Framework创建的Web服务。我只是设置测试环境来运行ASP文件。 –