C# 客户端GET请求没有返回任何内容?
我认为我的GET方法出了问题,因为当我尝试运行一段客户机代码时,没有得到任何返回 我的GET操作合同如下所示:C# 客户端GET请求没有返回任何内容?,c#,wcf,web-services,rest,C#,Wcf,Web Services,Rest,我认为我的GET方法出了问题,因为当我尝试运行一段客户机代码时,没有得到任何返回 我的GET操作合同如下所示: [OperationContract] [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, Uri
[OperationContract]
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "/Group/{TagName}")]
List<Group> GetGroupsCollection(string TagName);
public List<Group> GetGroupsCollection(string TagNames)
{
List<Group> groups = (from g in Groups
where
(from t in g.Tags where t.TagName == TagNames select t).Count() > 0
select g).ToList();
return groups;
}
private void AddTagetoGroup_Click(object sender, EventArgs e)
{
string uriAddTagtoGroup = string.Format("http://localhost:8000/Service/AddTagtoGroup/{0}/{1}", textBox6.Text, textBox7.Text);
byte[] arr = Encoding.UTF8.GetBytes(uriAddTagtoGroup);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriAddTagtoGroup);
req.Method = "POST";
req.ContentType = "application/xml";
req.ContentLength = arr.Length;
Stream reqStrm = req.GetRequestStream();
reqStrm.Write(arr, 0, arr.Length);
reqStrm.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
MessageBox.Show(resp.StatusDescription);
reqStrm.Close();
resp.Close();
}
string uriGetGroupsCollection = "http://localhost:8000/Service/Group/{TagName}";
从客户那里,这是这样做的:
[OperationContract]
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "/Group/{TagName}")]
List<Group> GetGroupsCollection(string TagName);
public List<Group> GetGroupsCollection(string TagNames)
{
List<Group> groups = (from g in Groups
where
(from t in g.Tags where t.TagName == TagNames select t).Count() > 0
select g).ToList();
return groups;
}
private void AddTagetoGroup_Click(object sender, EventArgs e)
{
string uriAddTagtoGroup = string.Format("http://localhost:8000/Service/AddTagtoGroup/{0}/{1}", textBox6.Text, textBox7.Text);
byte[] arr = Encoding.UTF8.GetBytes(uriAddTagtoGroup);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriAddTagtoGroup);
req.Method = "POST";
req.ContentType = "application/xml";
req.ContentLength = arr.Length;
Stream reqStrm = req.GetRequestStream();
reqStrm.Write(arr, 0, arr.Length);
reqStrm.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
MessageBox.Show(resp.StatusDescription);
reqStrm.Close();
resp.Close();
}
string uriGetGroupsCollection = "http://localhost:8000/Service/Group/{TagName}";
我得到的信息是好的,一切似乎都很好
现在我遇到一个问题的客户端代码是:
string uriGetGroupsCollection = "http://localhost:8000/Service/GetGroupsCollection/{TagName}";
private void button8_Click(object sender, EventArgs e)
{
string tagUri = uriGetGroupsCollection.Replace("{TagName}", textBox8.Text);
XDocument xDoc = XDocument.Load(tagUri); //this line gives 404 error not found.
var Tag = xDoc.Descendants("Group")
.Select(n => new
{
Tag = n.Element("GroupName").Value,
})
.ToList();
dataGridView3.DataSource = Tag;
}
这与我第一次提到的GET操作有关。因此,我不确定如何找出是客户机代码做错了什么,还是我实际的getgroupscolection
方法
因此,我的问题与向组中添加标记有关:
public void AddTagtoGroup(string group, string tag)
{
var result = Groups.Where(n => String.Equals(n.GroupName, tag)).FirstOrDefault();
if (result != null)
{
result.Tags.Add(new Tag() { TagName = tag });
}
}
或者它与GetGroupsCollection
的客户端代码相关
我更新了我的问题,以反映我以前遇到的小错误,surfen解决了这个错误(404错误),但这并没有解决我没有得到任何回报的问题 我认为您在URL中犯了一个错误:
string uriGetGroupsCollection = "http://localhost:8000/Service/GetGroupsCollection/{TagName}";
因为您是这样定义您的URITemplate的:“/Group/{TagName}”
或将模板更改为:
UriTemplate = "/GetGroupsCollection/{TagName}")]
更新
您的AddTagtoGroup
有另一个打字错误
var result = Groups.Where(n => String.Equals(n.GroupName, tag)).FirstOrDefault();
应该是:
var result = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();
希望这是好的、清晰的,并且布置得很好。如果有任何问题,请不要犹豫。您可以使用查看原始/文本客户端http请求/响应。您还可以使用Visual Studio调试器中断AddTagtoGroup等方法内的代码执行,检查变量,检查列表内容和程序流。发现得很好!没有404错误问题,但当我点击按钮时什么也没有发生,没有错误,也没有返回任何内容?