Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Silverlight handles binary data, but not POST

  • Loading branch information...
commit b8f524744edccbdc70ae502a09eb9cdfa95fd2a9 1 parent 51508e0
Erlend Oftedal authored
Showing with 31 additions and 28 deletions.
  1. +1 −1  README
  2. +30 −27 silverlight/SilverlightMalaRIA/MainPage.xaml.cs
View
2  README
@@ -22,5 +22,5 @@ the socket policy through the download port mentioned above.
Current limitations
-------------------
- The proxy runs the requests as a FIFO - not multithreaded
-- Silverlight does not handle binary data or POST
+- Silverlight does not handle POST
View
57 silverlight/SilverlightMalaRIA/MainPage.xaml.cs
@@ -69,15 +69,18 @@ public void OnReceive(object o, SocketAsyncEventArgs e)
try
{
Log("Trying: [" + match.Groups[2].Value + "]");
- var request = WebRequest.Create(new Uri(match.Groups[2].Value)) as HttpWebRequest;
- request.Method = match.Groups[1].Value;
- request.Accept = match.Groups[3].Value;
- var context = new Context {Request = request};
- if (request.Method == "POST")
+ var client = new WebClient();
+ if (match.Groups[1].Value == "GET")
{
- context.Data = match.Groups[5].Value;
+ client.OpenReadCompleted += OnReadCompleted;
+ client.OpenReadAsync(new Uri(match.Groups[2].Value));
+ } else
+ {
+ client.OpenWriteCompleted += OnWriteCompleted;
+ client.OpenReadCompleted += OnReadCompleted;
+ var context = new Context { Client = client, Data = match.Groups[5].Value };
+ client.OpenWriteAsync(new Uri(match.Groups[2].Value), "POST", context);
}
- request.BeginGetRequestStream(GetRequestStreamCallback, context);
}
catch(Exception ex)
{
@@ -90,33 +93,25 @@ public void OnReceive(object o, SocketAsyncEventArgs e)
HandleIncomingTraffic();
}
}
- public class Context
+
+
+ private void OnWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
{
- public HttpWebRequest Request;
- public string Data;
+ var context = (Context)e.UserState;
+ byte[] bytes = Encoding.UTF8.GetBytes(context.Data);
+ e.Result.Write(bytes, 0, bytes.Length);
+ Log("Wrote " + bytes.Length + " bytes as POST data");
+ e.Result.Flush();
+ //What to put here?
}
- private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
+ private void OnReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
- var context = (Context)asynchronousResult.AsyncState;
- var request = context.Request;
- Stream postStream = request.EndGetRequestStream(asynchronousResult);
-
- if (context.Data != null)
- {
- byte[] byteArray = Encoding.UTF8.GetBytes(context.Data);
-
- postStream.Write(byteArray, 0, byteArray.Length);
- }
- postStream.Close();
- request.BeginGetResponse(GetResponseCallback, request);
+ ReadFromStreamAndSendBack(e.Result);
}
- private void GetResponseCallback(IAsyncResult asynchronousResult)
+ private void ReadFromStreamAndSendBack(Stream streamResponse)
{
- var request = (HttpWebRequest)asynchronousResult.AsyncState;
- var response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
- Stream streamResponse = response.GetResponseStream();
var fullBuffer = new List<byte>();
var buffer = new byte[4096];
int length;
@@ -130,6 +125,14 @@ private void GetResponseCallback(IAsyncResult asynchronousResult)
Send(data.Length + ":", data, false);
}
+ public class Context
+ {
+ public WebClient Client;
+ public string Data;
+ }
+
+
+
public void OnSend(object o, SocketAsyncEventArgs e)
{
Log("Data sent back: " + e.Buffer.Length);
Please sign in to comment.
Something went wrong with that request. Please try again.