none
SilverLight2 beta2 的WebRequest不支持Delete和Put方法吗? RRS feed

  • 问题

  • 运行如下代码:

    Uri uri = new Uri("http://localhost:3000/people/1");
    HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
    // 设置请求类型为Delete,不支持该方法???
    request.Method = "DELETE";//运行到这里报不支持该方法,get和post可以
    。。。
    2008年9月3日 2:15

答案

全部回复

  • 不支持

    The Method property can only be set to GET and POST methods. Any other HTTP 1.1 or 1.0 protocol verbs (HEAD, PUT, DELETE, TRACE, or OPTIONS, for example) will throw a NotSupportedException exception.


    http://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest.method(en-us,VS.95).aspx
    2008年9月3日 3:24
    版主
  • 晕,那要用这两个方法怎么办,有没有其它替代的方案,谢谢!
    2008年9月3日 3:42
  • 你请求一个aspx页面来处理吧。
    2008年9月3日 3:45
    版主
  • 谢谢,你给我的链接里面有for Silverlight的,我在msdn里面查怎么没有
    2008年9月3日 3:56
  • 另:我用delete和put是要用REST方式访问Rails应用的,所以用aspx页面的方式是行不通的
    2008年9月3日 3:59
  •  fcw528 写:
    谢谢,你给我的链接里面有for Silverlight的,我在msdn里面查怎么没有

    本地的sdk是旧的,网上是最新的
    2008年9月3日 4:49
    版主
  • 最好是自己写HTTPserver来处理,用POST或GET,再加一个自定义的属性:Request.MyMethod="DELETE";进行处理即可。

    2008年9月9日 3:48
  • 最好是自己写HTTPserver来处理,用POST或GET,再加一个自定义的属性:Request.MyMethod="DELETE";进行处理即可。

    给你个HTTPserver的例子参考,我是用来搞服务器推送用的:

    http://hi.baidu.com/zhenyk/blog/item/91ca9c31159700ae5fdf0e88.html

    /////////////////////////////////开始////////////////////////////////////////

    namespace cnnbsun.webserver
    {
        using System;
        using System.IO;
        using System.Net;
        using System.Net.Sockets;
        using System.Text;
        using System.Threading;


        class MyWebServer
        {

            private TcpListener myListener;
            private int port = 8080; // 选者任何闲置端口

            //开始兼听端口
            //同时启动一个兼听进程
            public MyWebServer()
            {
                try
                {
                    //开始兼听端口
                    myListener = new TcpListener(IPAddress.Any, port);
                    myListener.Start();
                    Console.WriteLine("Web Server Running... Press ^C to Stop...");
                    //同时启动一个兼听进程 'StartListen'
                    Thread th = new Thread(new ThreadStart(StartListen));
                    th.Start();

                }
                catch (Exception e)
                {
                    Console.WriteLine("兼听端口时发生错误 :" + e.ToString());
                }
            }
            public void SendHeader(string sHttpVersion, string sMIMEHeader, int iTotBytes, string sStatusCode, ref Socket mySocket, string sRequest)
            {

                String sBuffer = "";

                if (sMIMEHeader.Length == 0)
                {
                    sMIMEHeader = "text/html"; // 默认 text/html
                }

                sBuffer = sBuffer + sHttpVersion + sStatusCode + "\r\n";
                sBuffer = sBuffer + "Server: cx1193719-b\r\n";
                sBuffer = sBuffer + "Content-Type: " + sMIMEHeader + "\r\n";
                sBuffer = sBuffer + "Accept-Ranges: bytes\r\n";
                if (sRequest.IndexOf("test.htm") > 0)
                {
                    sBuffer = sBuffer + "Content-type: multipart/x-mixed-replace;boundary=--ThisRandomString--\r\n\r\n";
                    sBuffer = sBuffer + "Content-Length: 100\r\n";
                }
                else
                {
                    sBuffer = sBuffer + "Content-Length: " + iTotBytes + "\r\n\r\n";
                }

     


                Byte[] bSendData = Encoding.ASCII.GetBytes(sBuffer);

                SendToBrowser(bSendData, ref mySocket, sRequest);

                Console.WriteLine("Total Bytes : " + iTotBytes.ToString());

            }

            public void SendToBrowser(String sData, ref Socket mySocket, string sRequest)
            {
                SendToBrowser(Encoding.ASCII.GetBytes(sData), ref mySocket, sRequest);
            }

            public void SendToBrowser(Byte[] bSendData, ref Socket mySocket, string sRequest)
            {
                int numBytes = 0;

                try
                {
                    if (mySocket.Connected)
                    {
                        if (sRequest.IndexOf("test.htm") > 0)
                        {
                            byte[] tmpData = System.Text.UTF8Encoding.UTF8.GetBytes("HTTP/1.1 200\r\nContent-type: multipart/x-mixed-replace;boundary=--ThisRandomString--\r\n\r\nShadow" + DateTime.Now.ToString());
                            mySocket.Send(tmpData);
                            System.Threading.Thread.Sleep(1000);
                            SendToBrowser(bSendData, ref mySocket, sRequest);
                        }
                        else
                        {
                            if ((numBytes = mySocket.Send(bSendData, bSendData.Length, 0)) == -1)
                                Console.WriteLine("Socket Error cannot Send Packet");
                            else
                            {
                                Console.WriteLine("No. of bytes send {0}", numBytes);
                            }
                        }


                    }
                    else
                        Console.WriteLine("连接失败....");
                }
                catch (Exception e)
                {
                    Console.WriteLine("发生错误 : {0} ", e);

                }
            }
            public static void Main()
            {
                MyWebServer MWS = new MyWebServer();
            }
            public void StartListen()
            {

                int iStartPos = 0;
                String sRequest;
                String sDirName;
                String sRequestedFile;
                String sErrorMessage;
                String sLocalDir;
                /////////////////////////////////////注意设定你自己的虚拟目录/////////////////////////////////////
                String sMyWebServerRoot = "D:\\temp\\"; //设置你的虚拟目录
                //////////////////////////////////////////////////////////////////////////////////////////////////
                String sPhysicalFilePath = "";
                String sFormattedMessage = "";
                String sResponse = "";


                while (true)
                {
                    //接受新连接
                    Socket mySocket = myListener.AcceptSocket();

                    Console.WriteLine("Socket Type " + mySocket.SocketType);
                    if (mySocket.Connected)
                    {
                        Console.WriteLine("\nClient Connected!!\n==================\nCLient IP {0}\n", mySocket.RemoteEndPoint);

                        Byte[] bReceive = new Byte[1024];
                        int i = mySocket.Receive(bReceive, bReceive.Length, 0);


                        //转换成字符串类型
                        string sBuffer = Encoding.ASCII.GetString(bReceive);


                        //只处理"get"请求类型
                        if (sBuffer.Substring(0, 3) != "POST")
                        {
                            Console.WriteLine("只处理get请求类型..");
                            mySocket.Close();
                            return;
                        }

                        // 查找 "HTTP" 的位置
                        iStartPos = sBuffer.IndexOf("HTTP", 1);


                        string sHttpVersion = sBuffer.Substring(iStartPos, 8);


                        // 得到请求类型和文件目录文件名
                        sRequest = sBuffer.Substring(0, iStartPos - 1);

                        sRequest.Replace("\\", "/");


                        //如果结尾不是文件名也不是以"/"结尾则加"/"
                        if ((sRequest.IndexOf(".") < 1) && (!sRequest.EndsWith("/")))
                        {
                            sRequest = sRequest + "/";
                        }


                        //得带请求文件名
                        iStartPos = sRequest.LastIndexOf("/") + 1;
                        sRequestedFile = sRequest.Substring(iStartPos);


                        //得到请求文件目录
                        sDirName = sRequest.Substring(sRequest.IndexOf("/"), sRequest.LastIndexOf("/") - 3);


                        //获取虚拟目录物理路径
                        sLocalDir = sMyWebServerRoot;

                        Console.WriteLine("请求文件目录 : " + sLocalDir);

                        if (sLocalDir.Length == 0)
                        {
                            sErrorMessage = "<H2>Error!! Requested Directory does not exists</H2><Br>";
                            SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found", ref mySocket, sRequest);
                            SendToBrowser(sErrorMessage, ref mySocket, sRequest);
                            mySocket.Close();
                            continue;
                        }


                        if (sRequestedFile.Length == 0)
                        {
                            // 取得请求文件名
                            sRequestedFile = "index.html";
                        }


                        /////////////////////////////////////////////////////////////////////
                        // 取得请求文件类型(设定为text/html)
                        /////////////////////////////////////////////////////////////////////

                        String sMimeType = "text/html";

                        sPhysicalFilePath = sLocalDir + sRequestedFile;
                        Console.WriteLine("请求文件: " + sPhysicalFilePath);


                        if (File.Exists(sPhysicalFilePath) == false)
                        {

                            sErrorMessage = "<H2>404 Error! File Does Not Exists...</H2>";
                            SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found", ref mySocket, sRequest);
                            SendToBrowser(sErrorMessage, ref mySocket, sRequest);

                            Console.WriteLine(sFormattedMessage);
                        }

                        else
                        {
                            int iTotBytes = 0;

                            sResponse = "";

                            FileStream fs = new FileStream(sPhysicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

                            BinaryReader reader = new BinaryReader(fs);
                            byte[] bytes = new byte[fs.Length];
                            int read;
                            while ((read = reader.Read(bytes, 0, bytes.Length)) != 0)
                            {
                                sResponse = sResponse + Encoding.ASCII.GetString(bytes, 0, read);

                                iTotBytes = iTotBytes + read;

                            }
                            reader.Close();
                            fs.Close();

                            SendHeader(sHttpVersion, sMimeType, iTotBytes, " 200 OK", ref mySocket, sRequest);
                            SendToBrowser(bytes, ref mySocket, sRequest);
                            //mySocket.Send(bytes, bytes.Length,0);

                        }
                        mySocket.Close();
                    }
                }
            }


        }

    }

    ///////////结束////////////////

    2008年9月9日 3:50
  • 呵呵,权益之计兄台这个思路倒是不失为一种方法
    另两个问题:
    1、                    //只处理"get"请求类型
                        if (sBuffer.Substring(0, 3) != "POST")
                        {
                            Console.WriteLine("只处理get请求类型..");
                            mySocket.Close();
                            return;
                        }
    是否应该修改为:
                        //只处理"get"请求类型
                        if (sBuffer.Substring(0, 3).ToUpper().Equals("POST")) {
                            Console.WriteLine("只处理get请求类型..");
                            mySocket.Close();
                            return;
                        }
    2、如果这样的话,目前这种同步方式是否应该修改异步方式,尽管你目前采用的线程方式进行处理?

    2008年9月11日 0:56
  •  

    兄弟,我纯属为了帮忙,这个只是雏形,真正要处理还要写一些代码,建议你看看HTTP协议的详细解释,在我的Blog里有。

    问题1:确实需要改,我的代码是个测试示例,算是抛砖引玉了;

    问题2:应该修改成异步方式,同时,还要考虑并发请求的处理。

    2008年9月15日 19:32
  • 学习一下,慢慢看

     

    2008年10月11日 5:18
    版主