从restful webservice获取响应

时间:2012-11-29 17:15:57

标签: c# asp.net web-services

我有以下代码连接到Web服务并查询API以获得成功结果。但我面临两个问题

  1. 我无法捕获XML格式的响应。

  2. 我无法从页面重定向回返回网址。

  3. 请帮助TIA

    string Url =“”;

        string Method = "";
    
        string Group = "";
    
        string FormName = "";
    
        string return_url = "";
    
        Url = "https://abc.com/ws/";
        Method = "getRates";
        Group = "rates";
        FormName = "form1";
        return_url = "~/app/Public/PaymentTest.aspx?DR={DR}";
    
        NameValueCollection FormFields = new NameValueCollection();
        FormFields.Add("username", "xxx");
        FormFields.Add("password", "xxxx");
        FormFields.Add("pin", "xxxx");
        FormFields.Add("dest_country", "Kenya");
        FormFields.Add("return_url", return_url);
    
        Response.Write("<html><head>");
        Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", FormName));
        Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", FormName, "post", Url + Group + "/" + Method));
    
        for (int i = 0; i < FormFields.Keys.Count; i++)
        {
            Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", FormFields.Keys[i], FormFields[FormFields.Keys[i]]));
        }
        Response.Write("</form>");
        Response.Write("</body></html>");
        Response.End();
    

2 个答案:

答案 0 :(得分:0)

将返回的xml加载到数据集中(注意:返回),然后我检索xml响应如下:

int i = 0;
string current = null;
for (i = 0; i <= returnds.Tables(0).Rows.Count - 1; i++) {
  if (Information.IsDBNull(returnds.Tables(0).Rows(i)("ValueOfXML")) == true) {
    current = "";} 
else {
    current = Convert.ToString(returnds.Tables(0).Rows(i)("ValueOfXML"));
  }
}

答案 1 :(得分:0)

您似乎正在尝试创建仅限HTML的解决方案。 (您使用ASP.NET生成HTML是一种分心)。您需要在onload上编写额外的javascript - 您可能不想提交表单,这将告诉浏览器您已完成页面。你想做一个javascript web服务调用,jquery方式比原始的XmlHttp更容易

参考:How to call a web service from jQuery