Reputation: 6655
I've created an aspx page that serves as a proxy to a WCF service hosted somewhere else on the intranet. The goal is to have a page that is not bound by cross site scripting restrictions and can work with any WCF web service.
I've got the call working and my debugger picks up the request but the data isn't being sent along and is null. I do have this working when not using a proxy (jquery ajax) when within same domain. Fiddler2 isn't picking up my HttpWebRequest for whatever reason so I can't see what's actually getting sent along.
Any ideas? Am I doing something wrong the way I'm posting the data? Any way to get fiddler to pick up the WebHttpRequest?
Proxy.aspx
// var data = Request.Form["d"];
// ProxyRequest r = JsonConvert.DeserializeObject<ProxyRequest>(data);
var r = new { ServiceName = "AccountService.svc", Type = "POST", MethodName = "AjaxFindCompany",
Data = "{\"AccountNumber\":null,\"Address\":{\"City\":\"Pittsburgh\",\"Country\":null,\"State\":\"PA\",\"Street1\":\"1 Ave\",\"ZipCode\":\"15222\"},\"BusinessName\":\"AUto\"}" };
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}?{3}", "http://localhost:2749", r.ServiceName, r.MethodName, r.Data));
request.Method = r.Type;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader contentReader = new StreamReader(response.GetResponseStream());
Response.ContentType = response.ContentType;
Response.Write(contentReader.ReadToEnd());
IAccountService.cs
[OperationContract]
[WebInvoke(Method = "*", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
MatchCompanyResponse AjaxFindCompany(MatchCompanyRequest request);
AccountService.cs
public MatchCompanyResponse FindCompany(MatchCompanyRequest request)
{
AccountHandler handler = new AccountHandler();
return handler.FindCompany(request);
}
public MatchCompanyResponse AjaxFindCompany(MatchCompanyRequest request)
{
// after calling proxy.aspx the debugger captures here but
// request is null...
return FindCompany(request);
}
Upvotes: 0
Views: 384
Reputation: 87228
In a POST request, the data goes in the body of the request, not in the URI. So the request should be something similar to this:
var r = new { ServiceName = "AccountService.svc", Type = "POST", MethodName = "AjaxFindCompany",
Data = "{\"AccountNumber\":null,\"Address\":{\"City\":\"Pittsburgh\",\"Country\":null,\"State\":\"PA\",\"Street1\":\"1001 Liberty Ave\",\"ZipCode\":\"15222\"},\"BusinessName\":\"AUto\"}" };
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}", "http://localhost:2749", r.ServiceName, r.MethodName));
request.Method = r.Type;
Stream reqStream = request.GetRequestStream();
byte[] reqBytes = Encoding.UTF8.GetBytes(r.Data as string);
reqStream.Write(reqBytes, 0, reqBytes.Length);
reqStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader contentReader = new StreamReader(response.GetResponseStream());
Response.ContentType = response.ContentType;
Response.Write(contentReader.ReadToEnd());
Upvotes: 1