c# - How to return WebClient() UploadFile() 302 Response -


just wondering if able answer question? attempting upload file webserver using webclient.uploadfile() function. working expected unable decode response due believe http communication between client , server ending in content-type image/jpeg.

var client = new webclient();             byte[] response = client.uploadfile("http://test.com", "post", "upload.jpg");             string s = system.text.encoding.utf8.getstring(response).tostring();             console.write(s); 

the http flow follows:

post / http/1.1 ---> http/1.1 100 continue <--- http/1.1 302 found <--- 

this contains url uploaded content.

get /5109799364591616 http/1.1 ---> http/1.1 200 ok <---- 

it seems program automatically following redirect meaning when return byte array contains image file.

is there anyway return 302 here? ultimate goal return redirected url , put user's clipboard.

for http-based requests, webclient uses httpwebrequest object instances internally. httpwebrequest allows enabling/disabling of automatic redirection via allowautoredirect property.

to access , tweak allowautoredirect property of httpwebrequest object, override protected method getwebrequest of webclient class. disable auto redirection, sufficient.

if server responds 302 (or similar code such 303), redirection url can found in location header field of response. fortunately, webclient provides public responseheaders collection easy access response header fields.

if need check actual response status code sent server, need have access httpwebresponse object generated httpwebrequest object. in similar fashion getting access httpwebrequest object, webclient has protected method can override access httpwebresponse object: getwebresponse(...). getwebresponse has overload used asynchronous operations. normally, overload should overridden in same manner.

a simple implementation of custom webclient class fitting basic needs similar this:

public class customwebclient : webclient {     public httpstatuscode? responsestatuscode { get; set; }       protected override webrequest getwebrequest(uri url)     {         webrequest req = base.getwebrequest(url);         if (req httpwebrequest)         {             ((httpwebrequest) req).allowautoredirect = false;         }          return req;     }      protected override webresponse getwebresponse(webrequest request)     {         webresponse resp = base.getwebresponse(request);         responsestatuscode = (resp httpwebresponse) ?             ((httpwebresponse) resp).statuscode             : (httpstatuscode?) null;         return resp;     }      protected override webresponse getwebresponse(webrequest request, iasyncresult result)     {         webresponse resp = base.getwebresponse(request, result);         responsestatuscode = (resp httpwebresponse) ?             ((httpwebresponse) resp).statuscode             : (httpstatuscode?) null;         return resp;     } } 

note property responsestatuscode in implementation above nullable type. http-based requests, property hold response status code of last request issued web client. other kind of requests (such ftp), value of property set null.

in application, use simple customwebclient class this:

var client = new customwebclient(); byte[] response = client.uploadfile("http://test.com", "post", "upload.jpg");  if (client.responsestatuscode == httpstatuscode.found) {     string redirecturi = client.responseheaders[httpresponseheader.location];      ... redirection uri ... } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -