FiddlerCore Without Registering As A System Proxy - fiddlercore

I have a .NET Application that I need to alter some WCF traffic headers on. The Microsoft folks told me there is no way to really inject the headers I need in the request and pick them up out of the response with the current framework. What I would like to do is just add FiddlerCore to the application, and if the header isn't there on the outgoing request from my application, then I would like to add it. (Real simple).
I can get everything to work, however the events only fire if I register the FiddlerApplication as a system proxy. I would like this transparent to the user so that it doesn't screw up their proxy settings in the OS.
private void Form1_Load(object sender, EventArgs e)
{
Fiddler.FiddlerApplication.SetAppDisplayName("FiddlerCoreTester");
Fiddler.FiddlerApplication.RequestHeadersAvailable += this.RequestHeadersAvailable;
Fiddler.FiddlerApplication.BeforeRequest += this.BeforeRequest;
Fiddler.FiddlerApplication.AfterSessionComplete += this.SessionComplete;
FiddlerApplication.OnNotification += this.OnNotification;
Fiddler.FiddlerApplication.ResponseHeadersAvailable += this.ResponseHeadersAvailable;
Fiddler.URLMonInterop.SetProxyInProcess("127.0.0.1:80", "<-loopback>");
Fiddler.FiddlerApplication.Startup(80, false, false);
WebClient wc = new WebClient();
string s = wc.DownloadString("http://www.google.com");
System.Windows.Forms.MessageBox.Show(s);
Fiddler.FiddlerApplication.Shutdown();
}
private void ResponseHeadersAvailable(Session oSession)
{
}
private void OnNotification(object sender, NotificationEventArgs e)
{
}
private void SessionComplete(Session oSession)
{
}
private void RequestHeadersAvailable()
{
}
private void BeforeRequest(Fiddler.Session oSession)
{
if (oSession.RequestHeaders.Exists("TESTHEADER") == false) {
oSession.RequestHeaders.Add("TESTHEADER", "TEST");
}
}
The events never get called in this case, however if I change this over to the below it does:
Fiddler.FiddlerApplication.Startup(80, true, false);
Does anyone know how to get this working?
Thanks so much

You should let your webclient use FiddlerCore as the proxy. You need to do this by setting the proxy of the webclient equal to proxy url that Fiddler listens to. Now your webclient loads the string through http://127.0.0.1:80 and Fiddler can capture your request.
WebClient wc = new WebClient();
wc.Proxy = new WebProxy(new Uri("http://127.0.0.1:80"));
string s = wc.DownloadString("http://www.google.com");
Fiddler.FiddlerApplication.Shutdown();

Related

Duplex WCF Client does not work with WinForms or WPF, but works fine with Console

I have a very peculiar case here.
I have implemented a wsDualHttpBinding WCF duplex server that is being consumed by a Windows Service.
The contract and implementation on the server side is defined as follows:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class FixServerContract : IFixContract
{
public FixServerContract()
{
}
public void Requestlogin(string message)
{
try
{
IServerCallback callback = OperationContext.Current.GetCallbackChannel<IServerCallback>();
callback.BroadcastToClient("Greetings from server");
}
catch (Exception ex)
{
}
}
[ServiceContract(CallbackContract=typeof(IServerCallback))]
public interface IFixContract
{
[OperationContract]
void Requestlogin(string message);
}
public interface IServerCallback
{
[OperationContract(IsOneWay = true)]
void BroadcastToClient(string eventData);
}
On the client end, I also have a single callback class, with the attached callback interface accessed by adding a Service Reference to the Client project as follows:
class MyCallbackClass : IFixContractCallback
{
public void RegisterClient()
{
InstanceContext context = new InstanceContext(this);
FixContractClient proxy = new FixContractClient(context);
proxy.Requestlogin("hello");
}
public void BroadcastToClient(string eventData)
{
}
}
When proxy.RequestLogin("hello"); is called by the client, the server should respond by signaling BroadcastToClient(string eventData)
But, here is where the peculiar behavior begins!
When my client calls RegisterClient directly from Main, outside of a Winform or WPF environment, everything works as it should be. That is, RequestLogin is successfully called to the server, and the server successfully responds by signalling BroadcastToClient on the client end.
This is shown here:
[STAThread]
private static void Main(string[] args)
{
MyCallbackClass callbackClass = new MyCallbackClass();
callbackClass.RegisterClient();
}
However, when I do the following below simply by wrapping the above lines inside an button_Click handler in the Winform, this bug manifests itself with a crash:
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
and then have a button click do the rest like so:
private void button1_Click(object sender, EventArgs e)
{
MyCallbackClass callbackClass = new MyCallbackClass();
callbackClass.RegisterClient();
}
then the following bug manifests itself:
1) RequestLogin is called on the server side, but the call to BroadcastToClient never reaches the client. Further to this, the RequestLogin never returns to client side, but instead, will timeout with the following exception:
2) An unhandled exception of type 'System.TimeoutException' occurred in mscorlib.dll. This request operation sent to XXXX did not receive a reply within the configured timeout (00:00:59.6359791). The time allotted to this operation may have been a portion of a longer timeout. This may be because the service is still processing the operation or because the service was unable to send a reply message. Please consider increasing the operation timeout (by casting the channel/proxy to IContextChannel and setting the OperationTimeout property) and ensure that the service is able to connect to the client.
The same behavior occurs if I use a WPF client. On the otherhand, it works perfectly if I use a Console based client!
Why does my Duplex Callback only work outside of a Winform or WPF?
How should I go about resolving this issue?
I am at my witts end here.
Thanks.
Kudos to this individual who has supplied me with an answer:
https://groups.google.com/forum/#!topic/microsoft.public.dotnet.framework.webservices/8Y6C8dRCFws
The solution is to modify MyCallbackClass as follows:
[CallbackBehavior(UseSynchronizationContext = false)]
class MyCallbackClass : IFixContractCallback
{
public void RegisterClient()
{
MyCallbackClass tester = this;
InstanceContext context = new InstanceContext(this);
FixContractClient proxy = new FixContractClient(context);
proxy.Requestlogin("hello");
}
public void BroadcastToClient(string eventData)
{
}
}

EPiServer XForm - Attaching file to an email

I have been trying to follow this blog http://world.episerver.com/Blogs/Anders-Hattestad/Dates/2013/1/Upload-within-Xform/
The form upload works, however the file does not appear in the email, but is uploaded onto the server, so its doing something.
The instructions I have are...
Just attach to the
BeforeSubmitPostedData event
Check if ((e.FormData.ChannelOptions & ChannelOptions.Email) != ChannelOptions.Email) is true
then send the custom mail and remove the send mail option
e.FormData.ChannelOptions &= ~ChannelOptions.Email;
If anyone could simplify this for me, would be appreciated...
Many Thanks.
Marc.
in Application_Start in your global.asax you need to attach to the XFormControl.ControlSetup Event
protected void Application_Start(object sender, EventArgs e)
{
XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
}
Then in your XForm_ControlSetup method, attach to the relevant event
public void XForm_ControlSetup(object sender, EventArgs e)
{
XFormControl control = (XFormControl)sender;
control.BeforeSubmitPostedData += new SaveFormDataEventHandler(XForm_BeforeSubmitPostedData);
}
And in your XForm_BeforeSubmitPostedData method
public void XForm_BeforeSubmitPostedData(object sender, SaveFormDataEventArgs e)
{
// Untested
if ((e.FormData.ChannelOptions & ChannelOptions.Email) != ChannelOptions.Email)
{
// Send custom mail here
// Remove the send mail option
e.FormData.ChannelOptions &= ~ChannelOptions.Email;
}
// End untested :)
}

silverlight exit call a Web service

I want to send data to the server when you close the application
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Exit(object sender, EventArgs e)
{
ClientReverse.UserExitGameAsync((Guid)Login);
}
Server:
public void UserExitGame(Guid UserGuid)
{
Games.Games.ExitUserGames(UserGuid);
}
but the server side is not satisfied.
It is already too late when you reach the ApplicationExit event.
I have seen Javascript that keeps on a webpage until confirmed (Stack Overflow does it a lot). You might want to modify a version of that Javascript that sends a message back to the Silverlight app before it allows page closing.
Calling Silverlight methods from JS is easy (you can simply expose SL methods to JS with the [Scriptable] attribute).

Problem with asynchronous webservice response

In my WP7 application i'm calling and consuming a webservice with these methods:
In my page .cs file:
public void Page_Loaded(object sender, RoutedEventArgs e)
{
if (NavigationContext.QueryString["val"] == "One")
{
listAgences=JSON.callWSAgence("http://...");
InitializeComponent();
DataContext = this;
}
}
In my json class i have these methods :
public List<Agence> callWSAgence(string url)
{
WebClient webClient = new WebClient();
Uri uri = new Uri(url);
webClient.OpenReadAsync(uri);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCompletedTestAgence);
return listAgences;
}
public void OpenReadCompletedTestAgence(object sender, OpenReadCompletedEventArgs e)
{
StreamReader reader = new System.IO.StreamReader(e.Result);
jsonResultString = reader.ReadToEnd().ToString();
addAgencesToList();
reader.Close();
}
public void addAgencesToList()
{
jsonResultString = json.Substring(5, json.Length - 6);
listAgences = JsonConvert.DeserializeObject<List<Agence>>(json);
}
The problem is that the OpenReadCompletedTest method in the json class is not called right after
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCompletedTestAgence);
So the listAgences returned is empty.
But later OpenReadCompletedTest is called and everything works fined, but my view has already been loaded.
What can i do to have a kind of synchronous call or to reload my view after the webservice response being parsed and my list being filled.
The behaviour (problem) you are seeing is because the web request is made asynchronously.
If you want to have a separate object call the web server this will need to handle a callback to process the response or make appropriate changes itself.
Also:
- the code in the question doesn't show what the variable json is defined as. In Page_Loaded it looks like a custom class but in OpenReadCompletedTestAgence and addAgencesToList it looks like a string.
- the code in Page_Loaded sets the value of listAgences twice.
check out the following question for more information about making asychronous calls synchrously Faking synchronous calls in Silverlight WP7

How can i pass an authentication cookie to an ADO.NET dataservice in Silverlight

I can pass a cookie into my silverlight application from another asp.net page, but i need to add it to the request header of my dataservice... This was easy in ASP.Net, but in Silverlight it seems that i can't manipulate the request header
private void Authorize()
{
Cookie dataServiceAuthCookie = new Cookie(HtmlPage.Document.QueryString["pass"],
HtmlPage.Document.QueryString["auth"]);
myDataService_Context.SendingRequest += new EventHandler<SendingRequestEventArgs>(Context_SendingRequest);
}
private void Context_SendingRequest(object sender, SendingRequestEventArgs e)
{
// what goes here?...
}
You can add headers to the request with SendingRequestEventArgs.RequestHeaders:
e.RequestHeaders["key"] = "value";

Categories

Resources