推薦這個blog:

Award


(ASP.NET 2010、2011、2012年)

其他資源

簡體中文blog

最新回應

[ASP.NET]在server端post資料到.ashx

前言
前陣子同事問我一個問題,要怎麼在server端post資料給遠端的.ashx。突然間我還真不知道怎麼做,因為通常都是在client端用ajax呼叫.ashx,如果是網站外的服務,通常都是Web service或WCF,.ashx還真沒碰到過。

所以survey了一下,寫了個小小Sample Code,當個memo。

需求
在server端post資料到.ashx,.ashx處理完後,接收.ashx的response。

範例
.aspx.cs

    protected void Button1_Click(object sender, EventArgs e)
    {

        var url = "http://localhost:13488/serverPostAshx/MyHandler.ashx";
        string uri = string.Format("{0}?who={1}", url, "joey");

        WebRequest request = WebRequest.Create(uri);
        request.Method = "POST";

        //加上"data=",讓server端可以透過Request.Form["data"]讀取
        string postData = "data={a:1, b:2, c:[3,4]}";

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        request.ContentType = "application/x-www-form-urlencoded";

        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;

        // Get the request stream.
        using (Stream dataStream = request.GetRequestStream())
        {
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
        }

        // Get the response.      
        using (WebResponse response = request.GetResponse())
        {
            using (Stream dataStream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(dataStream))
                {
                    string responseFromServer = reader.ReadToEnd();
                    this.Button1.Text = responseFromServer;
                }
            }
        }
    }

.ashx

public class MyHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        //Get QueryString
        var whom = context.Request.QueryString["who"];

        //取得整個Request傳過來的InputStream資料
        //var result = GetFromInputStream(context);

        //取得整個form資料
        //var result = HttpUtility.UrlDecode(context.Request.Form.ToString());

        var result = HttpUtility.UrlDecode(context.Request.Form["data"]);
        context.Response.Write(string.Format("{0} {1}", whom, result));
    }

    private static string GetFromInputStream(HttpContext context)
    {
        var reader = new System.IO.StreamReader(context.Request.InputStream);
        var result = reader.ReadToEnd();

        return result;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

說明

  1. 使用WebRequest,Method屬性使用Post。
  2. 將要post的資料,assign到WebRequest的InputStream這個屬性。
  3. 在.ashx中,就可以透過context.Request.InputStream來取得Request post過來的資料。
  4. 透過Encoding.UTF8.GetString就可以將InputStream還原成原本的字串。(可能是序列化後的字串)
  5. 若有經過序列化(xml or json)可以將字串反序列化後,操作物件。
  6. .aspx透過Request的GetResponse(),可以取得.ashx的回應。

 

結果畫面

  1. 按按鈕前
    image
  2. 按按鈕後
    image


結論
因為太少碰到這樣的需求了,所以memo一下,順便練習一下WebRequest的用法。如果大家有更好的建議作法,麻煩請不吝告知,謝謝。

參考

  1. HttpRequest.InputStream 屬性
  2. WebRequest.GetRequestStream 方法

 

Source CodeserverPostAshx.zip


勘誤1:感謝同事的提醒,送Request的方式一樣,只需要在post的data加上Form的Key。接著在server端就能透過Request.Form取得data的資料,經過UrlDecode後就是原本的資料。程式碼已經同步修改了,感謝提醒。

勘誤2:感謝黑大提醒,將原本讀取InputStream的部分,改為直接用StreamReader的ReadToEnd(),更為簡潔。程式碼已經同步修改了,感謝提醒。


點部落-In Joey

↑ Grab this Headline Animator


關連文章

[Tool]如何透過工具叫用與測試WCF的服務 - WcfTestClient

[Visual Studio]測試專案的分類

[.NET]Entity與DataTable互轉

[ASP.NET]套用MasterPage後,如何動態新增控制項

回應

  • # re: [ASP.NET]在server端post資料到.ashx by darknuminous

    請問,在一般編譯環境下可以執行,自行在 IIS 新增網站指定到相同的目錄,會出現 500 Internal Server Error,是為甚麼呢?已在 Web.config 裡新增

     

    <handlers>
          <add verb="*" path="*.ashx" name="MyHandler" type="MyHandler, MyHandlerAssembly" resourceType="Unspecified" />
        </handlers>

    2012/3/21 下午 06:03 | 回覆

  • # re: [ASP.NET]在server端post資料到.ashx by 91

    to darknuminous :

    不是很懂你的意思。
    也建議您到MSDN forum發問,forum的形式更適合發問,
    把前因後果描述清楚,會有很多人願意幫你。

    Forum網址:http://social.msdn.microsoft.com/Forums/zh-TW/236/threads/

    2012/3/21 下午 06:09 | 回覆

登入後使用進階評論

Please add 6 and 3 and type the answer here: