c# - Silverlight 4.0 : Using WebClient UploadStringAsync POST request to .Net 4 网络服务

标签 c# silverlight web-services .net-4.0 http-post

我正在尝试让这段代码运行:

Silverlight 应用程序 xaml.cs:

private void SavePoiRequest(MyPushpin pin)
    {
        WebClient wc = new WebClient();
        wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
        wc.Encoding = Encoding.UTF8;
        wc.UploadStringCompleted += new UploadStringCompletedEventHandler((sender, e) =>
        {
            if (e.Error != null)
            {
                return;
            }
            AddressTextBox.Text = e.Result;
        });
        String name = pin.Name;
        String lat  = pin.Location.Latitude.ToString().Replace(",",".");
        String lng  = pin.Location.Longitude.ToString().Replace(",",".");
        String address = pin.Address;
        String photodesc = pin.PhotoDesc;
        String poistory = pin.Tag.ToString();
        StringBuilder sr = new StringBuilder();
        sr.Append("createpoi?name="+name+ "&lat=" + lat + "&lng=" + lng + "&adr=" + address     + "&desc=" + photodesc + "&story=" + poistory);
        String parameter = sr.ToString();
        wc.UploadStringAsync(new Uri("http://localhost:80/"), "POST", parameter);
        AddressTextBox.Text = parameter;
    }

网络服务 CS:

[WebInvoke(UriTemplate = "createpoi?name={name}&lat={latitude}&lng={longitude}&adr={address}&desc={photodescription}&story={poistory}", Method = "POST")]
    public String SetPoiPOST(string name, string latitude, string longitude, string address, string photodescription, string poistory)
    {
        int newid = -1;
        POI_Man poimanager = new POI_Man();
        MemoryStream resultstream = new MemoryStream();

        if (!string.IsNullOrEmpty(name) &&
            !string.IsNullOrEmpty(latitude) &&
            !string.IsNullOrEmpty(longitude) &&
            !string.IsNullOrEmpty(address) &&                
            !string.IsNullOrEmpty(photodescription) &&
            !string.IsNullOrEmpty(poistory)
            )
        {
            //newid = poimanager.CreatePOI_alt(address, name, photodescription, poistory);
            newid = poimanager.CreatePOI(name, latitude, longitude, address, photodescription, poistory);
            poimanager.Generate("xml");
            resultstream = poimanager.WriteMessage(newid.ToString());
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
        }

        if (poimanager.NoError)
        {
            resultstream = poimanager.WriteMessage(newid.ToString());
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
            //resultstream = (this.GetPoi(newid.ToString()) as MemoryStream);
        }
        else
        {
            resultstream = poimanager.WriteMessage("Beim anlegen des POI ist ein Fehler aufgetreten."
                + Environment.NewLine + "Haben Sie einen Parameter vergessen?");
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";

        }
        return newid.ToString();
    }

Web 服务不会从客户端获取任何信息。我做错了什么吗?

我可以通过 WebClient OpenWriteAsync 的 POST 请求联系服务器。但是我需要服务器对 lastID 的响应,所以我改用 UploadStringAsync。 你能帮帮我吗?

再见 洲

最佳答案

尝试 wc.UploadStringAsync(new Uri("http://localhost:80/createpoi"), "POST", parameters) 并记住对您的参数进行 urlencode

关于c# - Silverlight 4.0 : Using WebClient UploadStringAsync POST request to .Net 4 网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6178918/

相关文章:

银光 2 : XamlParseException in Silverlight 2

java - 如何在 jax-rs 端点的路径参数中允许斜线

c# - 多个 LINQ 表达式和动态属性

c# - 使用作业对象限制用户处理器时间和查看虚拟内存

silverlight - 如何通过 Silverlight 发送 UDP 多播数据包?

silverlight - Silverlight工具包的现状和 future

c# - 长时间运行查询的进度条

c# - 二进制反序列化不同 .net 项目中的 vb.net 类

java - 在 android 中使用 SOAP 本地 Web 服务

.net - 如何让 WCF 自动关闭连接?