C# c、 Xamarin:执行webRequest.GetResponse()失败
我创建了一个类SoapClient,它创建了发送简单get和post请求的基本功能。但是,输出仅产生“bingo”和“System.Net.WebAsyncResult”,并且在SoapCLient的showDef函数中不继续。有什么问题吗?提前感谢…: SoapClient类 查找活动C# c、 Xamarin:执行webRequest.GetResponse()失败,c#,events,soap,xamarin,C#,Events,Soap,Xamarin,我创建了一个类SoapClient,它创建了发送简单get和post请求的基本功能。但是,输出仅产生“bingo”和“System.Net.WebAsyncResult”,并且在SoapCLient的showDef函数中不继续。有什么问题吗?提前感谢…: SoapClient类 查找活动 考虑从HtpWebRebug迁移到HTTPclipse。你在这里找到一个例子:很多人和XAMARIN一起遇到了HTTPWebRebug的问题。看看XAMARIN网站的例子——考虑从HTTPWebRebug到HT
考虑从HtpWebRebug迁移到HTTPclipse。你在这里找到一个例子:很多人和XAMARIN一起遇到了HTTPWebRebug的问题。看看XAMARIN网站的例子——考虑从HTTPWebRebug到HTTPclipse。您可以在这里找到一个例子:许多人在HttpWebRequest与Xamarin的结合方面存在问题-
using System;
using System.Xml;
using System.Net;
using System.IO;
using Android.OS;
namespace Dictionaryv3
{
public class SoapClient
{
private HttpWebRequest webRequest;
private Handler Dispatcher;
public delegate void soapDelegate (XmlDocument x);
public event soapDelegate responseReady;
public SoapClient() {}
public void SendGetRequest(String url, String action, string key, string value) {
Dispatcher = new Handler ();
url += "/" + action+ "?";
if (key != null) {
url+= key+"=" + value;
}
webRequest = CreateGetWebRequest(url);
webRequest.BeginGetResponse(new AsyncCallback (responseReceived), null);
}
private void responseReceived(IAsyncResult res) {
Console.WriteLine ("bingo");
Console.WriteLine (res.ToString ());
var result = webRequest.EndGetResponse (res);
Console.Write("received");
Console.Write(result.ToString());
Dispatcher.Post (() => {
Stream responseStream = result.GetResponseStream();
StreamReader sr = new StreamReader(result.GetResponseStream());
string xml = sr.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
if(responseReady != null) {
responseReady(doc);
}
});
}
public void SendPostRequest(String url, XmlDocument envelop) {
Dispatcher = new Handler ();
webRequest = CreatePostWebRequest(url);
InsertSoapEnvelopeIntoWebRequest(envelop, webRequest);
IAsyncResult asyncResult = webRequest.BeginGetResponse(responseReceived, null);
}
private void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
private HttpWebRequest CreateGetWebRequest(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "GET";
return webRequest;
}
private HttpWebRequest CreatePostWebRequest(String url) {
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
}
}
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Xml;
using System.Collections.ObjectModel;
using System.Collections.Generic;
namespace Dictionaryv3
{
[Activity (Label = "Dictionary", MainLauncher = true, Icon = "@drawable/icon")]
public class LookUpActivity : Activity
{
private Button LookUp;
private EditText Input;
private ListView WordView;
private List<Word> Words = new List<Word>();
private Button Menu;
private SoapClient client;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView( Resource.Layout.Main);
LookUp = ( Button ) FindViewById ( Resource.Id.lookup);
Input = (EditText)FindViewById (Resource.Id.input);
WordView = (ListView)FindViewById (Resource.Id.words);
Menu = (Button)FindViewById (Resource.Id.menu);
client = new SoapClient ();
client.responseReady += showDef;
Menu.Click += (object sender, EventArgs e) => {
PopupMenu menu = new PopupMenu (this, Menu);
menu.Inflate (Resource.Menu.popup_menu);
menu.Show ();
menu.MenuItemClick += (object send, PopupMenu.MenuItemClickEventArgs args) => {
string title = args.Item.TitleFormatted.ToString();
if(title == "Select dictionaries") {
setDictionaryActivity();
}
};
};
LookUp.Click += ( sender, eventArgs) => LookUpWord ();
}
private void setDictionaryActivity() {
var intent = new Intent(this, typeof(DictionarySelectionActivity));
StartActivity(intent);
}
private void LookUpWord() {
String inp = Input.Text;
if( inp != "") {
client.SendGetRequest("http://services.aonaware.com/DictService/DictService.asmx", "Define", "word", inp);
}
}
public void showDef(XmlDocument doc) {
Words.Clear();
XmlNodeList dlist = doc.GetElementsByTagName("Definition");
for(int i=0;i!=dlist.Count;i++) {
Word w = new Word();
w.dictionary = dlist.Item(i).ChildNodes.Item(1).LastChild.InnerText;
w.description = dlist.Item(i).ChildNodes.Item(2).InnerText;
Words.Add(w);
}
WordView.Adapter = new WordAdapter(this, Words);
}
protected override void OnPause() {
base.OnPause ();
}
protected override void OnResume() {
LookUpWord ();
base.OnResume ();
}
}
}