I'm struggling with a site I'm developing where I need to send an XML string to another site
http://ww1.caliber.ie/cram/api and receive the response. Could anyone tell me if I can do this in C# within the site and if so how? If not does anyone have
any suggestions?
Unsure what you mean by definition? Is it the xml string I need to pass to get a responce? If so it's:
var xmlString =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<request>' +
'<header>' +
'<userId>*GUEST</userId>' +
'<password>guest</password>' +
'</header>' +
'<body>' +
'<command>searchJobs</command>' +
'<parms>' +
// maxHits: for top five jobs put 5 here (integer)(optional)
'<parm>100</parm>' +
// space delimited list of vacancy statuses (required)
'<parm>Open</parm>' +
// space delimited list of vacancy categories (optional)
'<parm></parm>' +
// space delimited list of vacancy categories 2 (optional)
'<parm></parm>' +
// space delimited list of vacancy categories 3 (optional)
'<parm></parm>' +
// preferred location (optional)
'<parm></parm>' +
// preferred salary (integer)(optional)
'<parm></parm>' +
// list of keywords and/or double-quoted phrases (optional)
'<parm></parm>' +
'</parms>' +
'</body>' +
'</request>';
In normal scenario, the XML data can be transmited to other site via form post.
1. Create HTML with textarea or text box (ID=hiddenXMLData) and paste your XML
2. Set Form Action tag as target URL.
I have the two files below but I can't get them to work properly.
Proxy.asp:
<%
If "" <> Trim(Request("url")) Then
If "http"=LCase(Left(Trim(Request("url")),4)) Then
Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP")
objHTTP.open "GET", Request("url"), false
objHTTP.send
response.write objHTTP.ResponseText
Else
Response.Redirect Request("url")
End If
else
Response.Write "You did not provide a url"
End If
%>
test.html:
<html>
<head>
<title>XML Test</title>
<script type="text/javascript">
var xmlString =
'' +
'<request>' +
'<header>' +
'<userId>*GUEST</userId>' +
'<password>guest</password>' +
'</header>' +
'<body>' +
'<command>searchJobs</command>' +
'<parms>' +
// maxHits: for top five jobs put 5 here (integer)(optional)
'<parm>100</parm>' +
// space delimited list of vacancy statuses (required)
'<parm>Open</parm>' +
// space delimited list of vacancy categories (optional)
'<parm></parm>' +
// space delimited list of vacancy categories 2 (optional)
'<parm></parm>' +
// space delimited list of vacancy categories 3 (optional)
'<parm></parm>' +
// preferred location (optional)
'<parm></parm>' +
// preferred salary (integer)(optional)
'<parm></parm>' +
// list of keywords and/or double-quoted phrases (optional)
'<parm></parm>' +
'</parms>' +
'</body>' +
'</request>';
function openThroughProxy() {
var xmlhttp = GetXmlHttpObject();
if( xmlhttp)
{
var url = encodeURIComponent("http://ww1.caliber.ie/cram/api");
xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4){requestCompleted(xmlhttp.responseText)}};
xmlhttp.open('GET', 'proxy.asp?url=' + url, true);
xmlhttp.send(xmlString);
}
}
function requestCompleted(data){
alert("Data Received: " + data);
document.getElementById("fetchedData").value=data;
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body>
<input id="Button2" type="button" onclick="openThroughProxy();" value="button" />
<div><textarea rows="7" cols="80" id="fetchedData"></textarea></div>
</body>
</html>
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Marked as answer by Denis_dh on Aug 25, 2008 07:12 PM
Sorry, I'm not sure how I should wire this up to the test.html page? I understand that I need a proxy to send the data through to the 3rd party and get the response but I'm not really sure of what bits of the code need to be where and how to link it all
together.
Thanks Samu Zhang, when I got the time to look closer I figured out what was going on. My code is below for anyone else sticking there head into the world of Cross Domain fun and games.
Denis_dh
Member
160 Points
142 Posts
Send XML and get response in C#
Aug 20, 2008 07:29 PM|LINK
Hi,
I'm struggling with a site I'm developing where I need to send an XML string to another site http://ww1.caliber.ie/cram/api and receive the response. Could anyone tell me if I can do this in C# within the site and if so how? If not does anyone have
any suggestions?
Thanks
Denis
bhadelia.imr...
Contributor
3191 Points
533 Posts
Re: Send XML and get response in C#
Aug 20, 2008 08:01 PM|LINK
What is the defination to call this API? any documents?
[MCTS]
Born to fly High
http://knowledgebaseworld.blogspot.com/
Denis_dh
Member
160 Points
142 Posts
Re: Send XML and get response in C#
Aug 21, 2008 10:16 AM|LINK
Unsure what you mean by definition? Is it the xml string I need to pass to get a responce? If so it's:
var xmlString = '<?xml version="1.0" encoding="UTF-8"?>' + '<request>' + '<header>' + '<userId>*GUEST</userId>' + '<password>guest</password>' + '</header>' + '<body>' + '<command>searchJobs</command>' + '<parms>' + // maxHits: for top five jobs put 5 here (integer)(optional) '<parm>100</parm>' + // space delimited list of vacancy statuses (required) '<parm>Open</parm>' + // space delimited list of vacancy categories (optional) '<parm></parm>' + // space delimited list of vacancy categories 2 (optional) '<parm></parm>' + // space delimited list of vacancy categories 3 (optional) '<parm></parm>' + // preferred location (optional) '<parm></parm>' + // preferred salary (integer)(optional) '<parm></parm>' + // list of keywords and/or double-quoted phrases (optional) '<parm></parm>' + '</parms>' + '</body>' + '</request>';PSP_152890
Participant
783 Points
154 Posts
Re: Send XML and get response in C#
Aug 21, 2008 05:36 PM|LINK
1. Create HTML with textarea or text box (ID=hiddenXMLData) and paste your XML
2. Set Form Action tag as target URL.
Saravana Prakash P.
Denis_dh
Member
160 Points
142 Posts
Re: Send XML and get response in C#
Aug 22, 2008 01:33 PM|LINK
I have the two files below but I can't get them to work properly.
Samu Zhang -...
All-Star
62163 Points
6101 Posts
Re: Send XML and get response in C#
Aug 25, 2008 08:09 AM|LINK
Hi Denis_dh ,
Try my sample:
protected void Button1_Click(object sender, EventArgs e) { string xml = "<root>abc</root>"; string url = "http://localhost:20478/xxx.aspx"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); //string s = "id="+Server.UrlEncode(xml); byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xml); req.Method = "POST"; req.ContentType = "text/xml;charset=utf-8"; req.ContentLength = requestBytes.Length; Stream requestStream = req.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default); string backstr = sr.ReadToEnd(); sr.Close(); res.Close(); }Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Denis_dh
Member
160 Points
142 Posts
Re: Send XML and get response in C#
Aug 25, 2008 11:53 AM|LINK
Sorry, I'm not sure how I should wire this up to the test.html page? I understand that I need a proxy to send the data through to the 3rd party and get the response but I'm not really sure of what bits of the code need to be where and how to link it all together.
Denis_dh
Member
160 Points
142 Posts
Re: Send XML and get response in C#
Aug 25, 2008 07:11 PM|LINK
Thanks Samu Zhang, when I got the time to look closer I figured out what was going on. My code is below for anyone else sticking there head into the world of Cross Domain fun and games.
Now I just need to go off and learn XSL!
Cheers,
Denis
Default.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> "server"> <form id="<span" class="st">"form1" runat="server"> <div> "Button1" runat="server" Text="Run" onclick="Button1_Click" /> "TextBox1" runat="server" Height="400px" Width="600px"> </div> </form> Default.aspx.cs: using System; using System.Net; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string xml = "<?xml version='1.0' encoding='UTF-8'?><request><header><userId>*GUEST</userId><password>guest</password></header><body><command>searchJobs</command><parms><parm>100</parm><parm>Open</parm><parm></parm><parm></parm><parm></parm><parm></parm><parm></parm><parm></parm></parms></body></request>"; string url = "http://ww1.caliber.ie/cram/api"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); //string s = "id="+Server.UrlEncode(xml); byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xml); req.Method = "POST"; req.ContentType = "text/xml;charset=utf-8"; req.ContentLength = requestBytes.Length; Stream requestStream = req.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default); string backstr = sr.ReadToEnd(); TextBox1.Text = backstr; sr.Close(); res.Close(); } }