[英]Silverlight 4.0: Using WebClient UploadStringAsync POST request to .Net 4 Webservice
I'm trying to get this code to run:我试图让这段代码运行:
Silverlight App xaml.cs: 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;
}
Webservice cs:网络服务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();
}
The web service doesn't get nothing from the client. web 服务没有从客户端获得任何信息。 Am I doing something wrong?
难道我做错了什么?
I can contact the Server via a POST-request through WebClient OpenWriteAsync.我可以通过 WebClient OpenWriteAsync 通过 POST 请求联系服务器。 But I need the response of the server for the lastID, so I use UploadStringAsync instead.
但是我需要服务器对 lastID 的响应,所以我改用 UploadStringAsync。 Could you please help me?
请你帮助我好吗?
Bye Chau再见周
Try wc.UploadStringAsync(new Uri("http://localhost:80/createpoi"), "POST", parameters) and remember to urlencode your parameters尝试 wc.UploadStringAsync(new Uri("http://localhost:80/createpoi"), "POST", parameters) 并记住对参数进行 urlencode
thx for the advice.谢谢你的建议。 I now know how the REST service works.
我现在知道 REST 服务是如何工作的。 It only reads the URL and not the parameters of the UploadStringAsync() function.
它只读取 URL 而不是 UploadStringAsync() function 的参数。 So I rewrote the code and it works like a charm.
所以我重写了代码,它就像一个魅力。
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;
}
lastID = int.Parse(e.Result);
});
Uri baseAddressUri = new Uri("http://localhost:80/");
UriTemplate uriTemplate = new UriTemplate("createpoi?name={name}&lat={latitude}&lng={longitude}&adr={address}&desc={photodesc}&story={poistory}");
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("name", pin.Name);
parameters.Add("latitude", pin.Location.Latitude.ToString().Replace(",", "."));
parameters.Add("longitude", pin.Location.Longitude.ToString().Replace(",", "."));
parameters.Add("address", pin.Address);
parameters.Add("photodesc", pin.PhotoDesc);
parameters.Add("poistory", pin.Tag.ToString());
Uri formattedUri = uriTemplate.BindByName(baseAddressUri, parameters);
wc.UploadStringAsync(formattedUri, "POST");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.