我只是想在http://www.test.com/test.asp?test1=3上发表一篇http帖子.这是我一直在尝试使用的代码:
private void pif_test_conn() { Uri url = new Uri("http://www.test.com/test.asp?test1=3", UriKind.Absolute); if (httpResult == true) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; request.BeginGetResponse(new AsyncCallback(ReadCallback), request); } return ; } private void ReadCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream())) { string resultString = streamReader1.ReadToEnd(); MessageBox.Show("Using HttpWebRequest: " + resultString, "Found", MessageBoxButton.OK); } }
当我执行此代码时,我的程序会触发Application_UnhandledException事件.不确定我做错了什么.
你想张贴到另一个主人吗?这种行为可能导致XSS安全问题,因此不可用.
string responseValue = ""; AutoResetEvent syncRequest = new AutoResetEvent(false); Uri address = new Uri(HtmlPage.Document.DocumentUri, "/sample.aspx"); WebRequest request = WebRequest.Create(address); request.Method = "POST"; request.BeginGetRequestStream(getRequestResult => { // Send packet data using (Stream post = request.EndGetRequestStream(getRequestResult)) { post.Write(buffer, 0, buffer.Length); post.Close(); } // wait for server response request.BeginGetResponse(getResponseResult => { WebResponse response = request.EndGetResponse(getResponseResult); responseValue=new StreamReader(response.GetResponseStream()).ReadToEnd(); syncRequest.Set(); }, null); }, null); syncRequest.WaitOne(); MessageBox.Show( "Using WebRequest: " + responseValue, "Found", MessageBoxButton.OK);
HTH