Sys.Net.WebRequest总结


关于asp.net AJAX还会有很多。
 Sys.Net.WebRequest

提供客户端Script功能制造一个Web请求.


Constructors     

Sys.Net.WebRequest webRequest Constructor             

  初始画一个新的WebRequest class的实例.
var wRequest =  new Sys.Net.WebRequest();


Members
(1)Sys.Net.WebRequest add_completed Method              
结合Web请求实例注册一个事件,
wRequest.add_completed(handler);



(2)Sys.Net.WebRequest completed Method
结合Sys.Net.WebRequest事例添加完整的事件.


(3)Sys.Net.WebRequest.getResolvedUrl Method
得到一个Web请求实例的resolved  URL

(4)Sys.Net.WebRequest invoke Method                   
执行Web请求
Sys.Net.WebRequestManager.invoke(WebRequest);
异常:
Sys.InvalidOperationException        最多只能调用一次

如 果你已经注册completed request event handler为你的Sys.Net.WebRequest 实例,按照网络堆异步调用 handler的请求过程.一个WebRequest实例中只能调用一个invoke.如果网络请求已经发出就会抛出一个异常.                    



(5)Sys.Net.WebRequest remove_completed Method           
删除添加在Handler上的add_completed方法.
返回值必须是字符串,null,或一个XmlDocument对象实例.

(6)Sys.Net.WebRequest executor Property               
得到,设置结合Sys.Net.WebRequest实例的执行者


(7)Sys.Net.WebRequest body Property         
得到,设置一个Sys.Net.WebRequest实例的HTTP body.
只是在我们要自定义网络执行者的时候才使用.如果你不设置这个属性,
系统默认为Sys.Net.XmlHttpExecutor执行者,设置在调用invoke之后.
如果你的WebRequest实例已经与执行关联并且Web request已经发行.
如 果你尝试设置executor属性,就像这样                       
myRequest.set_executor(myRequest.get_executor())
就会有一个异常被抛出.
                       
var  body  =  MyWebRequest.get_body();
MyWebRequest.set_body(value);


(8)Sys.Net.WebRequest headers Property   
为Sys.Net.WebRequest实例得到HTTP headers,WebRequest发送的
HTTP headers是键值对的字典.headers属性返回为一个字典.
如果没有自定义headers,返回这个字典为空.你使用返回字典来设置你的自定义header 值.


(9)Sys.Net.WebRequest httpVerb Property   
得到或设置一个使用Sys.Net.WebRequest类的HTTP verb.
                       
注意,如果之前Http verb没有被设定.那就要在如果body属性为null,
WebRequest返回"GET",如果body属性为null,
WebRequest返回"POST".如果返回body是一个空字符串(""),
但其实不是null,返回的也应该是"POST"


(10)Sys.Net.WebRequest timeout Property   
设置或得到Sys.Net.WebRequest实例超时值.当去前的超时值是以毫秒值.
这个超时属性是表示在结合WebRequest实例网络执行者等待网络请求之前的超时时间.
"0"表示下一个网络堆立即输出.


(11)Sys.Net.WebRequest url Property       
得到或设置Sys.Net.WebRequest实例的URL.如果URL没被设置,返回值为空字符串("").
你能设置一个完整的URL,一个绝 对的URL,或相对的URL.
WebRequest能将这个值转换给network执行者使用.


(12)Sys.Net.WebRequest userContext Property   
得到或设置用户context.返回user context,userContext属性要在WebRequest调用invoke方法之前调用.
Web请求的completed handler能使用相同的userContext.
允许区别不同的事件handler调用者,或重新获得状态或当处理返回数据的context.


WebRequest类被使用在异步交流层中,制造一个Web请求.在你的应用程序中不用刻意使用这个类,而是在你需要在浏览器中调用Web service方法或静态页面方法按照使用代理方法时使用.

你能创建和使用一个WebRequest实例在下面场景中使用:

    当你要制造网络请求不是简单的Web service方法调用(就是这 是一种复杂的调用Web service的方法).
    当你要设置HTTP请求属性.
    当你使用自定义的执行者,要基于Sys.Net.WebRequestExecutor类.



下面是事例来于MSDN:
<% @ Page Language="C#"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >


< html   >
    
< head  id ="Head1"  runat ="server" >

        
< title >  Using WebRequest  </ title >

        
< style  type ="text/css" >
            body 
{}{  font: 11pt Trebuchet MS;
                    font-color
: #000000;
                    padding-top
: 72px;
                    text-align
: center }


            .text 
{}{ font: 8pt Trebuchet MS }
        
</ style >

   
</ head >

   
< body >

    
< h2 > Using WebRequest </ h2 >

        
< form  id ="form1"  runat ="server" >
            
< asp:ScriptManager  runat ="server"  ID ="ScriptManagerId" >
                
< Scripts >
                    
<asp:ScriptReference Path="WebRequest.js" />
                
</ Scripts >
            
</ asp:ScriptManager >
        
</ form >

        
< table >
            
< tr  align ="left" >
                
< td > Make GET Request: </ td >
                
< td >
                    
< button  id ="Button1"   
                        onclick
="GetWebRequest()" > GET </ button >
                
</ td >
            
</ tr >
            
< tr  align ="left" >   
                
< td > Request Body: </ td >
                
< td >
                    
< button  id ="Button2"   
                        onclick
="PostWebRequest()" > Body </ button >
                
</ td >

            
</ tr >
            
< tr  align ="left" >
                
< td > Request Timeout: </ td >
                
< td >
                    
< button  id ="Button3"   
                        onclick
="WebRequestTimeout()" > Timeout </ button >
                
</ td >
            
</ tr >  
            
< tr  align ="left" >
                
< td > Request Completed Handler: </ td >
                
< td >
                    
< button  id ="Button4"   
                        onclick
="WebRequestCompleted()" > Completed Handler </ button >
                
</ td >
            
</ tr >

            
< tr  align ="left" >
                
< td > Resolved Url: </ td >
                
< td >
                    
< button  id ="Button5"   
                        onclick
="GetWebRequestResolvedUrl()" > Resolved Url </ button >
                
</ td >

            
</ tr >

            
< tr  align ="left" >
                
< td > Request Executor: </ td >
                
< td >
                    
< button  id ="Button6"   
                        onclick
="WebRequestExecutor()" > Executor </ button >
                
</ td >

            
</ tr >

            
< tr  align ="left" >
                
< td > Request Header: </ td >
                
< td >
                    
< button  id ="Button7"   
                        onclick
="WebRequestHeader()" > Header </ button >
                
</ td >

            
</ tr >

        
</ table >


        
< hr  />

        
< div  id ="ResultId"  style ="background-color:Aqua;" ></ div >



    
</ body >

</ html >


var  getPage;
var  postPage;
var  displayElement;

function  pageLoad()
{
    getPage 
= "getTarget.htm";
    postPage 
= "postTarget.aspx";
    displayElement 
= $get("ResultId");
}


//  This function performs a GET Web request 
//
 to retrieve  information from the Url specified in 
//
 the query string. 
function  GetWebRequest()
{
    alert(
"Performing Get Web request.");

    
// Instantiate the WebRequest object.
    var wRequest =  new Sys.Net.WebRequest();

    
// Set the request Url.  
    wRequest.set_url(getPage);  

    
// Set the request verb.
    wRequest.set_httpVerb("GET");

    
// Set user's context
    wRequest.set_userContext("user's context");

    
// Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted);


    
// Clear the results page element.
    displayElement.innerHTML = "";

    
// Execute the request.
    wRequest.invoke();  

}


//  This function performs a POST Web request
//
 to upload information to the resource 
//
 identified by the Url. 
function  PostWebRequest()
{
    
// Instantiate the WebRequest object.
    var wRequest =  new Sys.Net.WebRequest();

    
// Set the request Url.  
    wRequest.set_url(postPage); 

    
// Set the request verb.
    wRequest.set_httpVerb("POST");

    
var body = "Message=Hello! Do you hear me?"
    wRequest.set_body(body);
    wRequest.get_headers()[
"Content-Length"= body.length;


    
// Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted);

    
// Clear the results page element.
    displayElement.innerHTML = "";

    
// Execute the request.
    wRequest.invoke();  
}


//  This function adds and removes the 
//
 Web request completed event handler.
function  WebRequestCompleted()
{
    
// Instantiate the WebRequest.
    var wRequest =  new Sys.Net.WebRequest();

    
// Set the request Url.  
    wRequest.set_url(getPage);  

    
// Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted);   
    alert(
"Added Web request completed handler");

    
// Remove the web request completed event handler.
    // Comment the following two lines if you want to
    // use the handler.
    wRequest.remove_completed(OnWebRequestCompleted); 
    alert(
"Removed handler; the Web request return is not processed.");

    
// Execute the request.
    wRequest.invoke();  
}


//  This function gets the resolved Url 
//
 of the Web request instance.
function  GetWebRequestResolvedUrl()
{
    
// Instantiate the WebRequest.
    var wRequest =  new Sys.Net.WebRequest();

    
// Set the request Url.  
    wRequest.set_url(getPage);  

    
// Get the web request completed event handler.
    var resUrl = wRequest.getResolvedUrl();   
    alert(
"Resolved Url: " + resUrl);

    
// Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted); 

    
// Execute the request.
    wRequest.invoke();  

}



//  This function gets and sets the 
//
 Web request time out.
function  WebRequestTimeout()
{    
    
// Instantiate the WebRequest.
    var wRequest =  new Sys.Net.WebRequest();

    
// Set the request Url.  
    wRequest.set_url(getPage);  

    
var defaultTimeout =  
        wRequest.get_timeout();

    
// Set request timeout to 100 msec.
    wRequest.set_timeout(100);

    
var newTimeout = 
        wRequest.get_timeout();

    alert(
"Default timeout: " + defaultTimeout);
    alert(
"New timeout: " + newTimeout);

    
// Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted);   

    
// Execute the request.
    wRequest.invoke();     
}



//  This function sets the Web request
//
 executor, replacing the default one.
function  WebRequestExecutor()
{    
    
// Instantiate the WebRequest.
    var wRequest =  new Sys.Net.WebRequest();

    
// Create the executor. In this case it is an
    // XMLHttpExecutor, equivalent to the default
    // executor. But, you can create a custom one.
    var executor = new Sys.Net.XMLHttpExecutor();

    
// Set the executor, replacing the default one. 
    // In this case the executor is equivalent to the
    // default one.
    wRequest.set_executor(executor); 

    
// Get the current executor       
    var executor =  
        wRequest.get_executor();

    alert(
"Response availabe: " + executor.get_responseAvailable())
}


 
//  This function sets an HTTP header for
  //  the Web request.
  function  WebRequestHeader() 
 
{
       
// Instantiate the WebRequest object.
    var wRequest =  new Sys.Net.WebRequest();

    
// Set the request Url.  
    wRequest.set_url(postPage); 

    
// Set the request verb.
    wRequest.set_httpVerb("POST");

    
var body = "Message=Hello! Do you hear me?"
    wRequest.set_body(body);

    
// Set the value of the HTTP header's "Content-Length".
    wRequest.get_headers()["Content-Length"= body.length;

    
// Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompletedHeader);

    
// Clear the results page element.
    displayElement.innerHTML = "";

    
// Execute the request.
    wRequest.invoke();  
}


//  This the handler for the Web request completed event
//
 that is used to display return data.
function  OnWebRequestCompleted(executor, eventArgs) 
{
    
if(executor.get_responseAvailable()) 
    
{

        
// Clear the previous results. 
        displayElement.innerHTML = "";

        
// Display Web request status.                  
        DisplayWebRequestStatus(executor);

        
// Display Web request headers.                  
        DisplayWebRequestHeaders(executor);

        
// Display Web request body.                  
        DisplayWebRequestBody(executor);

    }

    
else
    
{
        
if (executor.get_timedOut())
            alert(
"Timed Out");
        
else
            
if (executor.get_aborted())
                alert(
"Aborted");
    }

}



//  This the handler for the Web request completed event
//
 that is used to display header information.
function  OnWebRequestCompletedHeader(executor, eventArgs) 
{
    
if(executor.get_responseAvailable()) 
    
{

        
// Clear the previous results. 
         displayElement.innerHTML = "";

        
// Display Web request headers.                  
        DisplayWebRequestHeaders(executor);

    }

    
else
    
{

        
if (executor.get_timedOut())
            alert(
"Timed Out");

        
else

            
if (executor.get_aborted())
                alert(
"Aborted");

    }

}


//  This function is used to display the Web request status.
function  DisplayWebRequestStatus(executor)
{
     displayElement.innerHTML 
+=
     
"Status: [" + 
     executor.get_statusCode() 
+ " " + 
     executor.get_statusText() 
+ "]" + "<br/>"
}


//  This function is used to display Web request HTTP headers.
function  DisplayWebRequestHeaders(executor)
{
    displayElement.innerHTML 
+= 
        
"Headers: ";
    displayElement.innerHTML 
+= 
        executor.getAllResponseHeaders() 
+ "<br/>";
 }


//  This function is used to display the Web request body.
function  DisplayWebRequestBody(executor)
{   
     displayElement.innerHTML 
+= 
        
"Body: ";
    
if (document.all)
         displayElement.innerText 
+= 
            executor.get_responseData();
    
else
        
// Firefox
         displayElement.textContent += 
            executor.get_responseData();
}


//  This function is used to display the Web request message.
function  DisplayInformation(message)
{
    
// Clear the previous results.
     displayElement.innerHTML = "";
    
// Display information.
    displayElement.innerHTML = "<br/>" + message;
}


if  ( typeof (Sys)  !==   " undefined " ) Sys.Application.notifyScriptLoaded();
可能上面的着个实例有一点复杂..但是一个很好的实力。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值