2012-07-20 107 views
0

嗨,大家好我有這個代碼被創建爲一個Web服務,問題是WebRequest.Create,它似乎是一個Web服務沒有創建方法,什麼可以我做的解決方法?謝謝。這是我的整個代碼。如何在Web服務上調用WebReqeuest.Create

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); 

     } 

    } 
} 

}

回答

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); 確保你已經包括所需的頭文件。

+0

你面臨什麼問題? – 2012-07-20 05:31:07

+0

嗨,我需要能夠發送一個webrequest到一個特定的網址來檢查它的狀態,無論它是否正常運行,我的問題是\t'Test.Service.WebRequest'沒有包含'Create'的定義。 – anonymous1110 2012-07-20 05:36:15

+0

我更新了我的答案 – 2012-07-20 05:55:20