Upload audio file to soundcloud using c# - c#

WebClient client = new WebClient();
string postData = "client_id=" + "b408123adf1e3a950876d84475587ca2"
+ "&client_secret=" + "d74a342169f5f5b369622d582f77b09e"
+ "&grant_type=password&username=" + "biksad" //your username
+ "&password=" + "369789";//your password :)
string soundCloudTokenRes = "https://api.soundcloud.com/oauth2/token";
string tokenInfo = client.UploadString(soundCloudTokenRes, postData);
System.Net.ServicePointManager.Expect100Continue = false;
var request = WebRequest.Create("https://soundcloud.com/biksad/tracks") as HttpWebRequest;
//some default headers
request.Accept = "*/*";
request.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
request.Headers.Add("Accept-Language", "en-US,en;q=0.8,ru;q=0.6");
//file array
var files = new UploadFile[] { new UploadFile(filePath, "#" + fileName, "application/octet-stream") };
//other form data
var form = new NameValueCollection();
form.Add("track[title]", "biksad");
form.Add("track[sharing]", "public");
form.Add("oauth_token", tokenInfo);
form.Add("format", "json");
form.Add("Filename", fileName);
form.Add("Upload", "Submit Query");
string lblInfo;
try
{
using (var response = HttpUploadHelper.Upload(request, files, form))
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
lblInfo = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
lblInfo = ex.ToString();
}
I want to upload an audio file from my server to my soundcloud account. I got this error:
Cannot close stream until all bytes are written.
How can I detect "form" values correctly(track[title],track[sharing]...etc.)?

this link will show you what all the fields on that POST mean
http://developers.soundcloud.com/docs/api/reference#tracks
you can also use this tool they provide to look at what the URL should contain:
http://developers.soundcloud.com/console

Related

WebResponse does not utilize Passed In Cookies for Authentication

I am developing a C# console application to extract data from a PHP website. The website has both a public view and an extended view for logged in users. I am able to use my credentials, my username and password, to log in to the site via FireFox, Edge, and ID and view the additional material successfully. The parsing of the public data is not a problem, but the issue lies in extraction of the extended data.
I have tried using a variety of methods to create cookies based on my login credentials in order to access the extended view. My logic behind this is so: there is a login php page with a username and password. After successfully logging in, each subsequent access to a public page will display additional information (the extended data) if you are logged in.
I am trying to either pass my credentials via as part of the cookie container in the httpwebrequest or copy my existing cookies from firefox (after logging in) to my cookie container for my httpwebrequest. After that, I would use a HttpWebResponse object to view the returned data stream. It is there I should be able to identify the extended data in the html. However, to my knowledge, it has just returned the public view instead.
I am able to open the firefox cookies successfully but I don't know if they are being used successfully in the CookieContainer
//!!!!Firefox cookie method!!!!//
CookieContainer ffCookieContainer = new CookieContainer();
Cookie ffCookie = new Cookie();
bool fRtn = false;
string strPath, strTemp, strDb, strHost, strField, Value;
strPath = #"C:\Documents and Settings\YourUserName\Application Data\Mozilla\Firefox\Profiles\urq2tlhr.default\cookies.sqlite";
strDb = "Data Source=" + strPath;
// Now open the temporary cookie jar and extract Value from the cookie if we find it.
using (SqliteConnection conn = new SqliteConnection())
{
using (SqliteCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT host, name, value, path, expiry, isSecure, isHttpOnly, lastAccessed, id FROM moz_cookies;";
conn.ConnectionString = strDb;
conn.Open();
using (SqliteDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Value = reader.GetString(0) + " " + reader.GetString(1) + " " + reader.GetString(2) + " " + reader.GetString(3) + " " + reader.GetString(4) + " " + reader.GetString(5) + " " + reader.GetString(6) + " " + reader.GetString(7) + " " + reader.GetString(8);
if (!Value.Equals(string.Empty))
{
//fRtn = true;
//break;
try
{
ffCookie.Discard = false;
ffCookie.Expired = false;
ffCookie.Domain = reader.GetString(0);
ffCookie.Expires = Convert.ToDateTime("01/01/2025");
ffCookie.Domain = reader.GetString(0);
ffCookie.HttpOnly = false;
ffCookie.Secure = false;
ffCookie.Port = "";
ffCookie.Name = reader.GetString(1);
ffCookie.Path = reader.GetString(3);
ffCookie.Value = reader.GetString(2);
Console.WriteLine(Value);
ffCookieContainer.Add(ffCookie);
}
catch (Exception)
{
}
}
}
}
conn.Close();
}
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("website/view");
request.CookieContainer = ffCookieContainer;
//SECOND METHOD creating cookies based on credentials
string parameters = "username=blah#domain.com&password=blah";
string userName = "blah#domain.com";
string password = "blah";
string url = "example.com/index.php?forcelogin=1";
HttpWebRequest requestTwo = (HttpWebRequest)WebRequest.Create(url);
requestTwo.Method = "POST";
requestTwo.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
requestTwo.Headers.Add("Accept-Encoding: gzip,deflate");
requestTwo.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
requestTwo.KeepAlive = true;
requestTwo.Headers.Add("Keep-Alive: 900");
requestTwo.Referer = "example.com/index.php?forcelogin=1";
requestTwo.ContentLength = parameters.Length;
requestTwo.ContentType = "application/x-www-form-urlencoded";
requestTwo.CookieContainer = new CookieContainer();
using (Stream stream = requestTwo.GetRequestStream())
{
byte[] paramAsBytes = Encoding.Default.GetBytes(parameters);
stream.Write(paramAsBytes, 0, paramAsBytes.Count());
}
CookieContainer myContainer = new CookieContainer();
using (HttpWebResponse responseTwo = (HttpWebResponse)requestTwo.GetResponse())
{
foreach (var cookie in responseTwo.Cookies)
{
var properties = cookie.GetType()
.GetProperties()
.Select(p => new
{
Name = p.Name,
Value = p.GetValue(cookie)
});
foreach (var property in properties)
{
Console.WriteLine("{0}: {1}", property.Name, property.Value);
}
}
myContainer = requestTwo.CookieContainer;
}
//repeat the cookie reassignment to the web request for the public view of the web page
//READ THE RESPONSE
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
string data = readStream.ReadToEnd();
response.Close();
readStream.Close();
}
I expect to be able to see a newly generated html value in the page get response but I don't see it.

Problems with PayPal API Http call

I've integrated an option for users to pay via PayPal their online shopping on the web shop that I'm creating. The problem came up suddenly when I started to get this error:
You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.
And the code for the Http call is as following:
public string HttpCall(string NvpRequest)
{
string url = pEndPointURL;
string strPost = NvpRequest + "&" + buildCredentialsNVPString();
strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode);
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Timeout = Timeout;
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
try
{
using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
{
myWriter.Write(strPost.ToString());
}
}
catch (Exception e)
{
}
//Retrieve the Response returned from the NVP API call to PayPal.
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); // this is the line where the exception occurs...
string result;
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
}
return result;
}
Can someone help me out with this? It worked fine a day ago, now its giving me this error?
Okay so if anyone is interested, I was able to fix the error by adding the following line before creating the web request (I was able to fix it by going down to Tls12 like this):
`ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12`;
Cheers :-)
Edit try this:
public string HttpCall(string NvpRequest)
{
string url = pEndPointURL;
string strPost = NvpRequest + "&" + buildCredentialsNVPString();
strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
// Try using Tls11 if it doesnt works for you with Tls
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Timeout = Timeout;
objRequest.Method = WebRequestMethods.Http.Post;
objRequest.ContentLength = strPost.Length;
try
{
using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
{
myWriter.Write(strPost.ToString());
}
}
catch (Exception e)
{
}
//Retrieve the Response returned from the NVP API call to PayPal.
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
string result;
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
}
return result;
}
public string HttpCall(string NvpRequest) //CallNvpServer
{
string url = pendpointurl;
//To Add the credentials from the profile
string strPost = NvpRequest + "&" + buildCredentialsNVPString();
strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode( BNCode );
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Timeout = Timeout;
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
try
{
using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
{
myWriter.Write(strPost);
}
}
catch (Exception e)
{
/*
if (log.IsFatalEnabled)
{
log.Fatal(e.Message, this);
}*/
}
//Retrieve the Response returned from the NVP API call to PayPal
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
string result;
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
}
//Logging the response of the transaction
/* if (log.IsInfoEnabled)
{
log.Info("Result :" +
" Elapsed Time : " + (DateTime.Now - startDate).Milliseconds + " ms" +
result);
}
*/
return result;
}
After a number of hours wasted, it turned out to be the Tls protocol version.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Getting error : Could not create SSL/TLS secure channel

I am creating Shutterstock web application for searching images.
take a look on the code
function createImageComponent(image) {
var wrapper = $('<div>');
var thumbWrapper = $('<div>');
var thumbnail = $('<img>');
var description = $('<p>');
$(thumbnail).attr('src', image.assets.preview.url);
$(thumbWrapper)
.addClass('thumbnail-wrapper')
.css('height', image.assets.)
.css('width', image.assets.preview.width)
.append(thumbnail);
$(description)
.text(image.description)
.attr('title', image.description);
$(wrapper)
.addClass('media-wrapper image')
.append(thumbWrapper)
.append(description);
return wrapper;
}
// Create video wrapper component
function createVideoComponent(video) {
var wrapper = $('<div>');
var preview = $('<video>');
var description = $('<p>');
$(preview)
.attr('src', video.assets.thumb_mp4.url)
.attr('controls', true)
.attr('autoplay', true);
$(description)
.text(video.description)
.attr('title', video.description);
$(wrapper)
.addClass('media-wrapper video')
.append(preview)
.append(description);
return wrapper;
}
// Search media by type
function search(opts, media_type) {
var $container = $('#' + media_type + '-search-results');
var createComponentFunc = media_type === 'image' ? createImageComponent : createVideoComponent;
// Get Client ID and Client Secret for use in the Authorization header
var clientId = $('input[name=client_id]').val();
var clientSecret = $('input[name=client_secret]').val();
var jqxhr = $.ajax({
url: 'https://api.shutterstock.com/v2/' + media_type + 's/search',
data: opts,
headers: {
// Base 64 encode 'client_id:client_secret'
Authorization: 'Basic ' + window.btoa(clientId + ':' + clientSecret)
}
})
.done(function(data) {
if (data.total_count === 0) {
$container.append('<p>No Results</p>');
return;
}
$.each(data.data, function(i, item) {
var component = createComponentFunc(item);
$container.append(component);
});
})
.fail(function(xhr, status, err) {
alert('Failed to retrieve ' + media_type + ' search results:\n' + JSON.stringify(xhr.responseJSON, null, 2));
});
return jqxhr;
}
// On Page Load
$(function() {
$('#search-form').submit(function(e) {
e.preventDefault();
// Clear current media results
$('#image-search-results, #video-search-results').empty();
// Serialize form options
var opts = $("input[value != '']", this).serialize();
// Search and display images
search(opts, 'image');
// Search and display videos
search(opts, 'video');
return false;
});
});
This code is for searching images using shutterstock basic authentication.I want to create it using c#.
and i am using the following code :
LoadHttpPageWithBasicAuthentication(#"https://api.shutterstock.com/v2/images/232713811?view=full", "ClientID", "Clientsecrate");
private string LoadHttpPageWithBasicAuthentication(string url, string username, string password)
{
Uri myUri = new Uri(url);
WebRequest myWebRequest = HttpWebRequest.Create(myUri);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
NetworkCredential myNetworkCredential = new NetworkCredential(username, password);
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.Credentials = myCredentialCache;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream responseStream = myWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
string pageContent = myStreamReader.ReadToEnd();
responseStream.Close();
myWebResponse.Close();
return pageContent;
}
But i am getting the error
The request was aborted: Could not create SSL/TLS secure channel.
Please help what is the error stuck with this problem.
This was happening for me also for the shutterstock API .try to put this code
try
{
var request = (HttpWebRequest)WebRequest.Create("https://api.shutterstock.com/v2/images/232713811?view=full");
var username = "YourClientID";
var password = "YourClientSecrate";
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
request.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);
request.UserAgent = "MyApp 1.0";
var response = (HttpWebResponse)request.GetResponse();
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
// Image myojb = (Image)js.Deserialize(objText, typeof(Image));
var myojb = JsonConvert.DeserializeObject<RootObject>(objText);
// Response.Write(reader.ReadToEnd());
}
}
catch (WebException ea)
{
Console.WriteLine(ea.Message);
using (var stream = ea.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
}
}
You need to Deserialize the response data as what result you want.

the remote server error (401) Unauthorized while uploading a file using c#

I am trying to upload a file to server but its giving me 401 error and here is my 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 a space character between word "Basic" and authorization key when building Authorization header string:
client.Headers.Add(HttpRequestHeader.Authorization, "Basic " + auth);

File upload app is stuck

If I run my program and strUploadIP doens't exits in my network my whole application is stuck
FileInfo toUpload = new FileInfo(strFile);
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(#"ftp://" + **strUploadIP** + #"/" + strUser);
req.Method = WebRequestMethods.Ftp.MakeDirectory;
req.Credentials = new NetworkCredential(strUusername, strUpassword);
try
{
using (var resp = (FtpWebResponse)req.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
}
how can I get to try catch this or something?
You should add a try/catch to handle any errors.
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(#"ftp://" + **strUploadIP** + #"/" + strUser);
req.Method = WebRequestMethods.Ftp.MakeDirectory;
req.Credentials = new NetworkCredential(strUusername, strUpassword);
try {
using (var resp = (FtpWebResponse)req.GetResponse()) {
Console.WriteLine(resp.StatusCode);
}
} catch {
// TODO: Handle exception
}

Categories

Resources