简体   繁体   中英

Get response from restful webservice

I have the below code to connect to a webservice and query an API to get successful result. But I am facing two issues

  1. I am not able to capture the response which is in XML format.

  2. I am not able redirect from the page back to the return URL.

Please Help 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:

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. (That you are using ASP.NET to generate the HTML is a distraction). 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. You would want to make a javascript web service call, the jquery way is easier than the raw XmlHttp

Ref: How to call a web service from jQuery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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