在Windows窗体应用程序中使用REST WCF服务 [英] Consuming REST WCF Service in Windows Forms Application
本文介绍了在Windows窗体应用程序中使用REST WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在Windows窗体中使用rest wcf服务。如果是的话,请给我指示。我试过这样消费。
is it possible to consume rest wcf service in windows form. If so , please give me the direction. I tried like this to consume.
string ServiceUrl = "http://localhost:4664/MEKRest/Service.svc/news/";
WebRequest theRequest = WebRequest.Create(ServiceUrl);
WebResponse theResponse = theRequest.GetResponse();
DataContractSerializer collectionData = new DataContractSerializer(typeof(string));
var arrEmp = collectionData.ReadObject(theResponse.GetResponseStream());
label1.Text = arrEmp.ToString();
第1行位置错误50.期望来自命名空间的元素'string' http://schemas.microsoft.com/2003/10/Serialization/'..遇到名为'MEKNewsDataResponse'的'元素',命名空间'http://tempuri.org/'.
Error in line 1 position 50. Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element' with name 'MEKNewsDataResponse', namespace 'http://tempuri.org/'.
推荐答案
void CallBackJSONAddRequest(IAsyncResult ar)
{
//Contains information of the Async object containing Request Information
WebRequest req = (WebRequest)ar.AsyncState;
//Returns stream for writing data to the Web Resource
Stream reqStream = req.EndGetRequestStream(ar);
reqStream.Close();
//Now Collect the response Asynchronously
req.BeginGetResponse(CallBackJSONResponse, req);
}
//Response Callback, is mandatory to complete operations on the receiving end
void CallBackJSONResponse(IAsyncResult ar)
{
try
{
WebRequest req = (WebRequest)ar.AsyncState;
//Collect the reponse
WebResponse res = req.EndGetResponse(ar);
Stream strResult = res.GetResponseStream();
strResult.Close();
res.Close();
}
catch (Exception ex)
{
string s = ex.Message;
}
}
private void button1_Click(object sender, EventArgs e)
{
string uploadUrl = "http://localhost:7794/GetEmployees.svc/CreateEmployee/" + textBox1.Text + "/" + textBox2.Text + "/" + textBox3.Text;
WebRequest addRequest = WebRequest.Create(uploadUrl);
addRequest.Method = "POST";
//Begin the Asynchrnous Request
addRequest.BeginGetRequestStream(CallBackJSONAddRequest, addRequest);
label1.Text = "Employee" + textBox1.Text + "Inserted Successfuly";
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
这篇关于在Windows窗体应用程序中使用REST WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文