i trying upload file server giving me 401 error , here code.
static void main(string[] args) { string key = "ewh6r-u61eveh4kjrx4qizvjanru4qgz"; string input = @"e:\new folder\untitled_1.png"; string output = @"e:\new folder (2)\untitled_1.png"; string url = "https://api.tinify.com/shrink"; webclient client = new webclient(); // httpwebrequest client = (httpwebrequest)webrequest.create(url); // client.keepalive = false; // client.contenttype = "application/x-www-form-urlencoded"; // client.method = "post"; // client.timeout = system.threading.timeout.infinite; // client.credentials = new networkcredential("?", "?"); client.usedefaultcredentials = false; string auth = convert.tobase64string(encoding.utf8.getbytes("api:" + key)); client.headers.add(httprequestheader.authorization, "basic" + auth); //string input1 = @"key=gtthtxceq7shvgfl-afy_w_trnhhgml3&image=" + convert.tobase64string(file.readallbytes(input)); // string input3 = input1+auth; // byte[] bytes = encoding.utf8.getbytes(input1); // stream os = null; try { // client.contentlength = bytes.length; // os = client.getrequeststream(); // os.write(bytes, 0, bytes.length); client.uploaddata(url, file.readallbytes(input)); // client.uploadfile(url, "post", input); client.downloadfile(client.responseheaders["location"], output); } catch (exception e) { console.writeline("compression failed " + e.message); } }`
you missed space character between word "basic" , authorization key when building authorization header string:
client.headers.add(httprequestheader.authorization, "basic " + auth);
Comments
Post a Comment