简体   繁体   English

从宁静的Web服务获取响应

[英]Get response from restful webservice

I have the below code to connect to a webservice and query an API to get successful result. 我有以下代码连接到Web服务并查询API以获取成功结果。 But I am facing two issues 但是我面临两个问题

  1. I am not able to capture the response which is in XML format. 我无法捕获XML格式的响应。

  2. I am not able redirect from the page back to the return URL. 我无法从页面重定向回返回URL。

Please Help TIA 请帮助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();

Load the return xml into a dataset (note: returnds),and then I retrieve xml respones like this: 将返回的xml加载到数据集中(注意:returnds),然后我检索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"));
  }
}

You appear to be trying to create an HTML only solution. 您似乎正在尝试创建仅HTML解决方案。 (That you are using ASP.NET to generate the HTML is a distraction). (您正在使用ASP.NET生成HTML是一种干扰)。 You will need to write additional javascript on the onload-- you probably don't want to submit the form, that will tell the browser you are done with the page. 你将需要写额外的JavaScript的onload--你可能不希望提交表单,会告诉你与网页进行浏览器。 You would want to make a javascript web service call, the jquery way is easier than the raw XmlHttp 你想使一个JavaScript的Web服务调用,jQuery的方式比原为XmlHttp容易

Ref: How to call a web service from jQuery 参考: 如何从jQuery调用Web服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM