首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

Silverlight无法POST数据到服务器

2012-03-07 
求助:Silverlight无法POST数据到服务器webRequest.BeginGetRequestStream(new AsyncCallback(RequestReady

求助:Silverlight无法POST数据到服务器
webRequest.BeginGetRequestStream(new AsyncCallback(RequestReady), webRequest);
这句为什么总不能执行
附上RequestReady:
private void RequestReady(IAsyncResult asyncResult)
{
HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
Stream requestStream = request.EndGetRequestStream(asyncResult);
StreamWriter writer = new StreamWriter(requestStream ,Encoding.GetEncoding("gb2312"));
writer.Write(String.Format("data={0}",KeyTB.Text.Trim().ToString()));
writer.Flush();
request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
}

[解决办法]

C# code
WebClient wc = new WebClient();wc.DownloadStringAsync(uri);
[解决办法]
你的代码似乎有些问题,在Silverlight中实现POST有很多方法,你可以参考以下代码:

C# code
private void DoHTTPPost(List<string> stuffToPost)    {        syncContext = SynchronizationContext.Current;        HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( “URL address to POST to” ) );        request.Method = “POST”;        request.ContentType = “text/xml”;        request.BeginGetRequestStream( new AsyncCallback( DoHTTPPostRequestReady ), new HttpWebRequestData<List<string>>()        {            Request = request,            Data = stuffToPost        } );    }    private void DoHTTPPostRequestReady(IAsyncResult asyncResult)    {        HttpWebRequestData<List<string>> requestData = asyncResult.AsyncState as HttpWebRequestData<List<string>>;        HttpWebRequest request = requestData.Request;        Stream requestStream = request.EndGetRequestStream(asyncResult);        DataContractSerializer serializer = new DataContractSerializer(typeof(List<string>));        StreamWriter sw = new StreamWriter(requestStream);        serializer.WriteObject(requestStream, requestData.Data);        sw.Flush();        requestStream.Close();         request.BeginGetResponse(new AsyncCallback(DoHTTPPostResponseCallback), request);    }    private void DoHTTPPostResponseCallback(IAsyncResult ar)    {        HttpWebRequest request = ar.AsyncState as HttpWebRequest;        HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;        Stream responseStream = response.GetResponseStream();        DataContractSerializer serializer = new DataContractSerializer(typeof(string));        string result = serializer.ReadObject(responseStream) as string;        syncContext.Post(DoHTTPPostExtractResponse, new HttpWebResponseData<string>()        {            Response = response,            Data = result        });    }    private void DoHTTPPostExtractResponse(object state)    {        string response = (state as HttpWebResponseData<string>).Data;        txtBox.Text = response;    } 

热点排行