0

大家好,我有这个代码要创建为 Web 服务,问题出在 WebRequest.Create,似乎 Web 服务没有创建方法,我能做些什么作为解决方法?谢谢。这是我的整个代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using System.Net;
using System.IO;
using System.Net.NetworkInformation;
using System.Text;



namespace ApplicationMonitoring.Service
{
    /// <summary>
    /// Summary description for WebRequest
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebRequest : System.Web.Services.WebService 
    {

    [WebMethod]
    public string WebReq(string url,int timeout)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create(url);
            request.Proxy = new System.Net.WebProxy("10.80.50.60:8080", true);
            request.Timeout = timeout;
            //request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            // execute the request
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            response.StatusCode.ToString();

        }
        catch (WebException ex)
        {
            Response.Write(((HttpWebResponse)ex.Response).StatusCode);

        }

    }
}

}

4

1 回答 1

1

在您的代码HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);中创建一个 webService。

你可以参考这个链接http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

下面是创建 webRequest 实例的代码。

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
      public static void Main ()
      {
        // Create a request for the URL.        
        WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
        // If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials;
        // Get the response.
        HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
        // Display the status.
        Console.WriteLine (response.StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream ();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader (dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd ();
        // Display the content.
        Console.WriteLine (responseFromServer);
        // Cleanup the streams and the response.
        reader.Close ();
        dataStream.Close ();
        response.Close ();
       }
     }
   }

现在要创建一个 webService 请求,您只需使用

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 确保您已包含所需的头文件。

于 2012-07-20T05:30:34.200 回答