Windows phone 7 Windows Phone POST方法WebClient类
我是这个论坛以及Windows Phone开发的新手。我目前正在开发一个应用程序,其中我正在使用一个Web服务,我需要向一个Web服务发出POST请求。 我正在尝试实现一个用户登录功能, ->(URI) ->(参数) apikey:32字节长的字母数字 用户名:3-15个字符 密码:3-15个字符 为此,我尝试使用WebClient类的UploadStringSync方法来发布数据。我的代码如下Windows phone 7 Windows Phone POST方法WebClient类,windows-phone-7,post,webclient,Windows Phone 7,Post,Webclient,我是这个论坛以及Windows Phone开发的新手。我目前正在开发一个应用程序,其中我正在使用一个Web服务,我需要向一个Web服务发出POST请求。 我正在尝试实现一个用户登录功能, ->(URI) ->(参数) apikey:32字节长的字母数字 用户名:3-15个字符 密码:3-15个字符 为此,我尝试使用WebClient类的UploadStringSync方法来发布数据。我的代码如下 WebClient wc1 = new WebClient(); wc1.UploadStringA
WebClient wc1 = new WebClient();
wc1.UploadStringAsync(new Uri("http://abc.com/login"),"POST","?apikey=" + Apikey + "&username=username&password=password");
wc1.UploadStringCompleted += new UploadStringCompletedEventHandler(wc1_UploadStringCompleted);
void wc1_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
MessageBox.Show(e.Result);
}
执行在MessageBox行停止,并抛出一条消息说“远程服务器返回了一个错误:NotFound”
我传递参数的方式有问题吗?我试着到处寻找工作的实现,但找不到它
有人能帮我吗?这是我项目的一个起点,在这一点上我真的需要帮助。任何帮助都将不胜感激。试试以下方法:
public void Post(string address, string parameters, Action<string> onResponseGot)
{
Uri uri = new Uri(address);
HttpWebRequest r = (HttpWebRequest)WebRequest.Create(uri);
r.Method = "POST";
r.BeginGetRequestStream(delegate(IAsyncResult req)
{
var outStream = r.EndGetRequestStream(req);
using (StreamWriter w = new StreamWriter(outStream))
w.Write(parameters);
r.BeginGetResponse(delegate(IAsyncResult result)
{
try
{
HttpWebResponse response = (HttpWebResponse)r.EndGetResponse(result);
using (var stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
onResponseGot(reader.ReadToEnd());
}
}
}
catch
{
onResponseGot(null);
}
}, null);
}, null);
}
public void Post(字符串地址、字符串参数、操作onResponseGot)
{
Uri=新的Uri(地址);
HttpWebRequest r=(HttpWebRequest)WebRequest.Create(uri);
r、 方法=“POST”;
r、 BeginGetRequestStream(委托(IAsyncResult请求)
{
var outStream=r.EndGetRequestStream(请求);
使用(StreamWriter w=新StreamWriter(扩展))
w、 写入(参数);
r、 BeginGetResponse(委托(IAsyncResult结果)
{
尝试
{
HttpWebResponse=(HttpWebResponse)r.EndGetResponse(结果);
使用(var stream=response.GetResponseStream())
{
使用(StreamReader=新StreamReader(stream))
{
onResponseGot(reader.ReadToEnd());
}
}
}
抓住
{
onResponseGot(空);
}
},空);
},空);
}
我这样做了,效果很好
WebClient web = new WebClient();
web.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
web.UploadStringAsync((new Uri("http://www.something.com/?page=something")), "POST", string.Format("v1=onevalue&v2=anothervalue"));
web.UploadStringCompleted += web_UploadStringCompleted;
在上传完成以获得我使用的html html后,您可以使用e.Result获得整个html
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(e.Result);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//body//table");
MessageBox.Show(node.InnerText);
可能是你的
Url
有问题。您可以尝试添加www
和绝对UriKind
:新Uri(“http://WWW.abc.com/login“,UriKind.Absolute)
操作onResponseGot参数需要是什么?