2009-11-16 42 views
3
问题

我使用的HttpWebRequest将数据发布到银光3.0 Web服务器,这里是我的代码HttpWebRequest.EndGetRequestStream在Silverlight

public void MakePostRequest() 
    { 


      // Create a new HttpWebRequest object. 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.mywebsite.com/somepage.php");  

      // Set the ContentType property. 
      request.ContentType="application/x-www-form-urlencoded"; 
      // Set the Method property to 'POST' to post data to the URI. 
      request.Method = "POST"; 

      // Start the asynchronous operation.  
      request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);  

      // Keep the main thread from continuing while the asynchronous 
      // operation completes. A real world application 
      // could do something useful such as updating its user interface. 
      allDone.WaitOne(); 

      // Get the response. 

      request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);  
    } 


    private static void ResponseCallback(IAsyncResult asynchronousResult) 
    { 
     HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 
     HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
     Stream streamResponse = resp.GetResponseStream(); 
     StreamReader streamRead = new StreamReader(streamResponse); 
     string responseString = streamRead.ReadToEnd(); 
     Console.WriteLine(responseString); 
     // Close the stream object. 
     streamResponse.Close(); 
     streamRead.Close(); 

     // Release the HttpWebResponse. 
     resp.Close(); 
    } 

    private static void ReadCallback(IAsyncResult asynchronousResult) 
    {  
      IDictionary<string, string> objDictionary = new Dictionary<string, string>(); 
      objDictionary.Add("action", "login"); 
      objDictionary.Add("login", "[email protected]"); 
      objDictionary.Add("password", "pass123"); 


      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 
      // End the operation. 
      // This line of code making the blocking call??? 
      Stream postStream = request.EndGetRequestStream(asynchronousResult); 

     string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x"); 

      Stream memStream = new System.IO.MemoryStream(); 

      byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); 

      string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}"; 

      foreach (KeyValuePair<string, string> entry in objDictionary) 
      { 
       string key = entry.Key; 
       string value = entry.Value; 
       string formitem = string.Format(formdataTemplate, key, value); 
       byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); 
       memStream.Write(formitembytes, 0, formitembytes.Length); 
      } 

     memStream.Write(boundarybytes, 0, boundarybytes.Length); 



     memStream.Position = 0; 
     byte[] tempBuffer = new byte[memStream.Length]; 
     memStream.Read(tempBuffer, 0, tempBuffer.Length); 

     //Writing the name value pair 
     postStream.Write(tempBuffer, 0, tempBuffer.Length); 

     memStream.Close(); 
     postStream.Close(); 

} 

我现在面临的问题是该行Stream postStream = request.EndGetRequestStream(asynchronousResult);正在采取一些阻塞调用,而我的整个应用程序似乎都被吊死了..但是我可以从浏览器打开相同的网页...为什么这样呢?

回答

0

看来,既然有这个,这不是所有的代码:

allDone.WaitOne(); 

这将是你的阻塞调用的来源最有可能的。

3

删除您使用等待句柄allDone。请将您的电话转到BeginGetResponse,直到ReadCallback方法结束。实际上您链中的电话: -

BeginGetRequest->ReadCallback->BeginGetResponse->ResponseCallback 

顺便说一句,你正在使用的 “应用程序/ x-WWW的形式,进行了urlencoded” 内容类型。不过,您似乎试图对Multipart实体主体进行编码,在这种情况下,您的内容类型应该是“multipart/form-data”。

0

如果您只想将POST和名称/值对集合收集到服务器,只需使用WebClient即可。在两行代码中,你可以实现你想要的。

当然,它不会给你细粒度的控制,就像能够实现异步一样,但最终它会完成工作。

+0

请问你让我知道在Silverlight中发布数据到URL的两行代码? – 2009-11-19 12:50:38

0

似乎或可能是,您将访问需要“许可”的URL。 Web浏览器存储“cookie”或任何存在您的访问权限,但您的应用程序不是。一旦您使用浏览器登录到该URL,您只能获得您的浏览器应用程序的访问权限,并且不能派生到其他应用程序。