Https POST from Php to C# - c#

I have to make a post to a third party https url to get data procesed and sent back. And all I have as an example is this:
$signature= foo_string;
$data_to_post = json_dictionary;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HTTPHEADER,array("JSON-Signature: $signature"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_to_post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
As we work with ASP .NET C# 2.0, I have to port this, but I get always a not autenticated error.
Here is what I'm doing:
HttpWebRequest q = (HttpWebRequest)WebRequest.Create(Host + ":" + Port);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(new interhanse().AcceptAllCertifications);
q.Method = "POST";
q.Headers.Add("JSON-Signature:" + GetSignature(data));
q.ContentType = "application/json";
q.UseDefaultCredentials = false;
q.Credentials = new NetworkCredential(user,pwd, Host);
byte[] buffer = UTF8Encoding.UTF8.GetBytes(data);
q.ContentLength = data.Length;
Stream oStream = q.GetRequestStream();
StreamWriter oWriter = new StreamWriter(oStream);
oWriter.Write(buffer);
oWriter.Close();
HttpWebResponse reps = q.GetResponse() as HttpWebResponse;
I've read all SO questions I can find about this, but I don't get any improvements. Thanks in advance!

Well, one thing you're doing wrong is assuming that the length in bytes is the same as the length in characters. You should use buffer.Length for the content length. You're also calling StreamWriter.Write with a byte array. You shouldn't do that - you should just use the stream, as you've already done the encoding:
byte[] buffer = Encoding.UTF8.GetBytes(data);
q.ContentLength = buffer.Length;
using (Stream stream = q.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
Now, that won't solve the authentication issue. You may find that just setting PreAuthenticate solves that though:
q.PreAuthenticate = true;
If that doesn't work, I suggest you run WireShark and look at the differences between the request through Curl and the request from .NET.

I think you should not supply the host in the authentication...
q.Credentials = new NetworkCredential(user,pwd);
Which would be something like:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Host + ":" + Port);
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(new interhanse().AcceptAllCertifications);
request.Method = "POST";
request.Headers.Add("JSON-Signature:" + GetSignature(data));
request.ContentType = "application/json";
request.UseDefaultCredentials = false;
request.Credentials = new NetworkCredential(user, pwd);
byte[] buffer = UTF8Encoding.UTF8.GetBytes(data);
request.ContentLength = buffer.Length;
using (Stream oStream = request.GetRequestStream()) {
oStream.Write(buffer, 0, buffer.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
// load data from response here
}
Also you should avoid assigning the service point validation delegate on each request, this may slow down the requests increasingly because the validation is performed multiple times, and it's also kind of a memory leak.

curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
Here is how you add that CURLOPT_USERPWD in Asp.Net:
private async Task<string> Execute(string url, string query, string user, string pasword)
{
HttpClient httpClient = new HttpClient();
var baseUri = new Uri(url, UriKind.Absolute); // e.g. http://somedomain.com/endpoint
Uri request = new Uri(baseUri, query); // with query e.g. http://somedomain.com/endpoint?arg1=xyz&arg2=abc
// Add a new Request Message
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, request);
// add headers -> CURLOPT_USERPWD equivalent
var encodedStr = Convert.ToBase64String(Encoding.Default.GetBytes(string.Format("{0}:{1}", user, password)));
var authorizationKey = "Basic" + " " + encodedStr; // Note: Basic case sensitive
requestMessage.Headers.Add("Authorization", authorizationKey);
// if POST - do this instead
// content
//HttpContent content = new StringContent(jsonContent); // string jsonContent i.e. JsonConvert.SerializeObject(YourObject);
//requestMessage.Content = content;
//requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
// execute
HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
var responseString = await responseMessage.Content.ReadAsStringAsync(); // reads it as string;
// if json and you need to convert to an object do this
// var myresponse = JsonConvert.DeserializeObject<YourMappedObject>(responseString);
return responseString;
}

Related

Twitter API Chunked Upload INIT authentication error (code 32)

Im having trouble getting the INIT post to https://upload.twitter.com/1.1/media/upload.json to authenticate. It's returning "message":"Could not authenticate you","code":32
I have the simple upload for images working fine, and can publish messages too, and im using the same code to create the Authorazation header etc. The auth header is as follows (with my consumer key and auth token)
OAuth oauth_consumer_key="[my consumer key]", oauth_token="[my auth token]", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1671623331", oauth_nonce="NjM4MDcyMjAxMzExODczNDAx", oauth_version="1.0", oauth_signature="NqC6Fwrz763C8397%2FL67crijtZs%3D"
I've tried passing in the required values (command, total_bytes and media_type) in the body and on the query string, and i've tried including them when generating the signature too. I just cant see the cause of the issue.
var baseFormat_pic = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&command={6}&total_bytes={7}&media_type={8}";
var baseString_pic = string.Format(baseFormat_pic,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
"INIT",
fileBytes.ToString(),
HttpUtility.UrlEncode( "video/mp4"));
baseString_pic = string.Concat("POST&", Uri.EscapeDataString(resource_url_pic),
"&", Uri.EscapeDataString(baseString_pic));
var compositeKey_pic = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
"&", Uri.EscapeDataString(oauth_token_secret));
string oauth_signature_pic;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey_pic)))
{
oauth_signature_pic = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString_pic)));
}
var headerFormat_pic = "OAuth oauth_consumer_key=\"{3}\", oauth_token=\"{4}\", oauth_signature_method=\"{1}\", oauth_timestamp=\"{2}\", oauth_nonce=\"{0}\", oauth_version=\"{6}\", oauth_signature=\"{5}\"";
var authHeader_pic = string.Format(headerFormat_pic,
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_signature_pic),
Uri.EscapeDataString(oauth_version)
);
ServicePointManager.Expect100Continue = false;
string reqUrl = resource_url_pic;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reqUrl);
request.Method = "POST";
NameValueCollection nvc = new NameValueCollection();
nvc.Add("command", "INIT");
nvc.Add("total_bytes", fileBytes.ToString());
nvc.Add("media_type", "video/mp4");
var sb = new StringBuilder();
foreach (string key in nvc.Keys)
{
sb.AppendFormat("{0}={1}&", key, HttpUtility.UrlEncode(nvc[key].ToString()));
}
sb.Remove(sb.Length - 1, 1);
var bytes = Encoding.UTF8.GetBytes(sb.ToString());
request.ContentLength = bytes.Length;
request.Headers.Add("Authorization", authHeader_pic);
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
var stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
string result = ReadResult(request);
Am I missing something really obvious?
To solve this I changed from form url encoded to multipart form data. This allowed the INIT command to work correctly.

c# equivalent of md5 in PHP

I have a code which looks like this, It's basically a JSON request which I need to pass to the server and code in PHP looks like this:
<?php
$client_secret= '';
$data= array(
'email' => '**********',
'password' => '******',
'client_id' => '*******'
);
$api_url='******';
$json_data=json_encode($data);
$signature_string = md5($json_data . $client_secret);
$post_data = 'signature='.$signature_string.'&data='.urlencode($json_data);
$curl = curl_init($api_url);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($curl);
print_r($result);
curl_close($curl);
?>
And this works fine in PHP. So now I'm trying to translatet this code into C#.
What I did so far is:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
string client_secret = "*****";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"email\":\"******\"," +
"\"password\":\"******\"}" +
"\"client_id\":\"******\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
string resp = string.Empty;
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
resp = streamReader.ReadToEnd();
}
return resp;
Basically I now have my data in JSON format. What now I need to do is replicate the PHP's md5 function to create a MD5 hash out of my client_secret string + JSON string which was created in previous step and then simply post the data string to server.
Can someone help me out with this ?

How to make a Post request from one website to another?

Lets say I have two websites, Website-A and Website-B.
Website-A receives a post request (From a third party website), I want to relay this request along with its data to website-B. I tried the below approach but it doesn't seems to navigate to the website B.
string newUrl = "http://localhost/WebSite-B/Test.aspx";
HttpRequest original = HttpContext.Current.Request;
HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(newUrl);
newRequest.ContentType = original.ContentType;
newRequest.Method = original.HttpMethod;
newRequest.UserAgent = original.UserAgent;
byte[] originalStream;
using (var memoryStream = new MemoryStream())
{
original.InputStream.CopyTo(memoryStream);
originalStream = memoryStream.ToArray();
}
Stream reqStream = newRequest.GetRequestStream();
reqStream.Write(originalStream, 0, originalStream.Length);
reqStream.Close();
newRequest.CookieContainer = new CookieContainer();
newRequest.GetResponse();
The GetResponse() doesn't seem to open up or navigate to the requested page.
Please help me to pass on the POST request received by Website-A to Website-B.
I have answered a similar question in another web site. For your convinience here is a sample code:
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "now=" + strId;
postData += ("&random=" + random);
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(http://www.domain.com/controler/action);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
An alternative method to acomplish the same functionality can be found here

Post string and bytearray to server

I have done POSTING and GETing from server using string post data, but now i have the next WebService:
submitFeedback(String token, String subject, String body, byte[] photo, byte[] video);
private void PostFeedbackData()
{
if (GS.online != true)
{
MessageBox.Show(GS.translations["ErrorMsg0"]);
}
else
{
HttpWebRequest request = HttpWebRequest.CreateHttp("https://****/feedback");
request.Method = "POST";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
}
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
// End the stream request operation
Stream postStream = myRequest.EndGetRequestStream(callbackResult);
// Create the post data
string postData = "";
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
postData = "{\"jsonrpc\": \"2.0\", \"method\": \"getUserSchedule\", \"params\":[" + "\"" + (App.Current as App).UserToken + "\",\"" + FeedbackTitle.Text + "\",\"" + FeedbackContent.Text + "\",\"" + imageBytes + "\"], \"id\": 1}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
});
}
Look at post data - it is a string, i can't place there imageBytes array. How to do it?
Is there any reason for using HttpWebRequest ?
I am asking that because i think that the correct way of sending files to a service is by multipartform data. With HttpWebRequest you have to manually implement that.
Here is an example that is using Microsoft's HttpClient that might work for you:
public async Task<string> submitFeedback(String token, String subject, String body, byte[] photo, byte[] video) {
var client = new HttpClient();
var content = new MultipartFormDataContent();
// Some APIs do not support quotes in boundary field
foreach (var param in content.Headers.ContentType.Parameters.Where(param => param.Name.Equals("boundary")))
param.Value = param.Value.Replace("\"", String.Empty);
var tok = new StringContent(token);
content.Add(tok, "\"token\"");
var sub = new StringContent(subject);
content.Add(tok, "\"subject\"");
// Add the Photo
var photoContent = new StreamContent(new MemoryStream(photo));
photoContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"photo\"",
FileName = "\"photoname.jpg\""
};
photoContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
content.Add(photoContent);
// Add the video
var videoContent = new StreamContent(new MemoryStream(video));
videoContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"video\"",
FileName = "\"videoname.jpg\""
};
videoContent.Headers.ContentType = MediaTypeHeaderValue.Parse("video/mp4");
content.Add(videoContent);
HttpResponseMessage resp;
try {
resp = await client.PostAsync("SERVER_URL_GOES_HERE", content);
}
catch (Exception e)
{
return "EXCEPTION ERROR";
}
if (resp.StatusCode != HttpStatusCode.OK)
{
return resp.StatusCode.ToString();
}
var reponse = await resp.Content.ReadAsStringAsync();
return reponse;
}
Change accordingly.
Note: HttpClient is also using HttpWebRequest under the hood.
Also I don't think its a good idea to have the UI thread making the request. That makes no sense since you are blocking the interface and making the async theory useless.

How to upload video on Dailymotion with c# ?? Is somebody has a complete code?

I wish upload a video on dailymotion with c# code , but dailymotion doesn't provide c# code to upload video with c#.
I search on dailymotion documentation api and i found this not explicit curl code :
curl -F 'access_token=...' \
-F 'url=http://upload-02.dailymotion.com/files/5ccb48b8e8aef3fcb8959739f993e1b9.3gp' \
https://api.dailymotion.com/me/videos
and i tryed to transpose but it's not working:
string contentFile = "c:\name_of_my_video_file.flv";
byte[] byteArray = Encoding.ASCII.GetBytes(contentFile);
MemoryStream fs = new MemoryStream(byteArray);
// Provide the WebPermission Credintials
// Create a request using a URL that can receive a post.
string uri = "https://api.dailymotion.com/me/videos";
WebRequest request = WebRequest.Create(uri);
// Set the Method property of the request to POST.
request.Method = "POST";
request.Credentials = new NetworkCredential("logindailymotion","passworddailymotion");
// Create POST data and convert it to a byte array.
string postData = "access_token=my_api_key&url=http://upload-02.dailymotion.com/files/name_of_my_video_file.flv";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
// Notify the server about the size of the uploaded file
request.ContentLength = fs.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
//FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = request.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch (Exception ex)
{
throw ex;
}
}
Is there a better documentation for c# code ?
Is somebody has the correct code ?
I created a complete example to answer your question and upload a video. It is available on GitHub now.
Edit: I used API information available explaining how to do their OAuth Authentication and Video Publishing.
Here is all the code except for the objects that are deserialized from JSON:
Code
static void Main(string[] args)
{
var accessToken = GetAccessToken();
Authorize(accessToken);
Console.WriteLine("Access token is " + accessToken);
var fileToUpload = #"C:\Program Files\Common Files\Microsoft Shared\ink\en-US\join.avi";
Console.WriteLine("File to upload is " + fileToUpload);
var uploadUrl = GetFileUploadUrl(accessToken);
Console.WriteLine("Posting to " + uploadUrl);
var response = GetFileUploadResponse(fileToUpload, accessToken, uploadUrl);
Console.WriteLine("Response:\n");
Console.WriteLine(response + "\n");
Console.WriteLine("Publishing video.\n");
var uploadedResponse = PublishVideo(response, accessToken);
Console.WriteLine(uploadedResponse);
Console.WriteLine("Done. Press enter to exit.");
Console.ReadLine();
}
private static UploadResponse GetFileUploadResponse(string fileToUpload, string accessToken, string uploadUrl)
{
var client = new WebClient();
client.Headers.Add("Authorization", "OAuth " + accessToken);
var responseBytes = client.UploadFile(uploadUrl, fileToUpload);
var responseString = Encoding.UTF8.GetString(responseBytes);
var response = JsonConvert.DeserializeObject<UploadResponse>(responseString);
return response;
}
private static UploadedResponse PublishVideo(UploadResponse uploadResponse, string accessToken)
{
var request = WebRequest.Create("https://api.dailymotion.com/me/videos?url=" + HttpUtility.UrlEncode(uploadResponse.url));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("Authorization", "OAuth " + accessToken);
var requestString = String.Format("title={0}&tags={1}&channel={2}&published={3}",
HttpUtility.UrlEncode("some title"),
HttpUtility.UrlEncode("tag1"),
HttpUtility.UrlEncode("news"),
HttpUtility.UrlEncode("true"));
var requestBytes = Encoding.UTF8.GetBytes(requestString);
var requestStream = request.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
var response = request.GetResponse();
var responseStream = response.GetResponseStream();
string responseString;
using (var reader = new StreamReader(responseStream))
{
responseString = reader.ReadToEnd();
}
var uploadedResponse = JsonConvert.DeserializeObject<UploadedResponse>(responseString);
return uploadedResponse;
}
private static string GetAccessToken()
{
var request = WebRequest.Create("https://api.dailymotion.com/oauth/token");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var requestString = String.Format("grant_type=password&client_id={0}&client_secret={1}&username={2}&password={3}",
HttpUtility.UrlEncode(SettingsProvider.Key),
HttpUtility.UrlEncode(SettingsProvider.Secret),
HttpUtility.UrlEncode(SettingsProvider.Username),
HttpUtility.UrlEncode(SettingsProvider.Password));
var requestBytes = Encoding.UTF8.GetBytes(requestString);
var requestStream = request.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
var response = request.GetResponse();
var responseStream = response.GetResponseStream();
string responseString;
using (var reader = new StreamReader(responseStream))
{
responseString = reader.ReadToEnd();
}
var oauthResponse = JsonConvert.DeserializeObject<OAuthResponse>(responseString);
return oauthResponse.access_token;
}
private static void Authorize(string accessToken)
{
var authorizeUrl = String.Format("https://api.dailymotion.com/oauth/authorize?response_type=code&client_id={0}&scope=read+write+manage_videos+delete&redirect_uri={1}",
HttpUtility.UrlEncode(SettingsProvider.Key),
HttpUtility.UrlEncode(SettingsProvider.CallbackUrl));
Console.WriteLine("We need permissions to upload. Press enter to open web browser.");
Console.ReadLine();
Process.Start(authorizeUrl);
var client = new WebClient();
client.Headers.Add("Authorization", "OAuth " + accessToken);
Console.WriteLine("Press enter once you have authenticated and been redirected to your callback URL");
Console.ReadLine();
}
private static string GetFileUploadUrl(string accessToken)
{
var client = new WebClient();
client.Headers.Add("Authorization", "OAuth " + accessToken);
var urlResponse = client.DownloadString("https://api.dailymotion.com/file/upload");
var response = JsonConvert.DeserializeObject<UploadRequestResponse>(urlResponse).upload_url;
return response;
}
Again, I put it on GitHub.

Categories

Resources