标签:post httpwebrequest silverlight-3-0 c silverlight
我正在使用HTTPWebRequest在Silver light 3.0中将数据发布到Web服务器,这是我的代码
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", "myid@yahoo.com");
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);正在进行一些阻止调用,并且我的整个应用程序似乎都已挂起..但是我可以从浏览器中打开同一网页…为什么会这样?
解决方法:
删除对Wait句柄allDone的使用.而是将对BeginGetResponse的调用移到ReadCallback方法的末尾.实际上,您可以将电话链接起来:
BeginGetRequest->ReadCallback->BeginGetResponse->ResponseCallback
顺便说一句,您正在使用“应用程序/ x-www-form-urlencoded”的内容类型.但是,您似乎正在尝试对Multipart实体主体进行编码,在这种情况下,您的内容类型应为“ multipart / form-data”.
标签:post,httpwebrequest,silverlight-3-0,c,silverlight 来源: https://codeday.me/bug/20191210/2101526.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。