1

I control both sites so any method will do.

There must be a simpler way then the following:

byte[] result;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://blahblah.com/blah.ashx");
byte[] inputToSend = new byte[] { 1, 2, 3 };
request.Method = "POST";
request.ContentType = "image/jpeg";
request.Timeout = 30 * 1000;
request.ContentLength = inputToSend.Length;
using (Stream stream = request.GetRequestStream())
    stream.Write(inputToSend, 0, inputToSend.Length);
request.Headers.Add("blah", "more blah");//This is for authentication.
WebResponse r = request.GetResponse();
using (MemoryStream ms = new MemoryStream())
{
    r.GetResponseStream().CopyTo(ms);
    result = ms.ToArray();
}

Not so?

(The code is the requesting side. The responder is simpler.)

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • possible duplicate of [How to read a WebClient response after posting data? (.NET)](http://stackoverflow.com/questions/1014935/how-to-read-a-webclient-response-after-posting-data-net) – Dour High Arch Nov 26 '13 at 01:25

1 Answers1

1

You could potentially use the WebClient to make the code smaller. Specifically, the UploadData method:

using (var wc = new WebClient()) {
    wc.UploadData(yourUrl, inputToSend);
}

..and to download:

using (var wc = new WebClient()) {
    var receivedData = wc.DownloadData(yourUri);
}

You can add whatever headers you need via the WebClient Headers property:

wc.Headers.Add("blah", "blah"); // your auth stuff here.
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • Thanks. That looks promising. But is there a way to request+response with WebClient? (The received data is in response to the sent data) – ispiro Nov 25 '13 at 23:11
  • OK. Never mind - I found this http://stackoverflow.com/a/1014944/939213 showing that `UploadData` returns a response. – ispiro Nov 25 '13 at 23:12