Converting WebClient to HttpClient - c#

I have been surfing and working on this for hours now and I'm read to throw my PC.
I have a WebClient on WPF application that needs to be converted to HttpClient for UWP application. I am getting a lot of header errors and since I'm not a web guru I am beating myself.
Here's the original and if someone would be extremely kind and help me convert it to HttpClient I would be very thankful.
using (var client = new WebClient())
{
string request = "------WebKitFormBoundarygWsJMIUcbjwBPfeL"
+ Environment.NewLine
+ "Content-Disposition: form-data; name=\"guid\""
+ Environment.NewLine
+ Environment.NewLine
+ presetSmall.PresetId.ToString()
+ Environment.NewLine
+ "------WebKitFormBoundarygWsJMIUcbjwBPfeL"
+ Environment.NewLine
+ "Content-Disposition: form-data; name=\"delay\""
+ Environment.NewLine
+ Environment.NewLine
+ "0"
+ Environment.NewLine
+ "------WebKitFormBoundarygWsJMIUcbjwBPfeL--"
+ Environment.NewLine;
client.Headers.Add(HttpRequestHeader.ContentType, "multipart/form-data; boundary=----WebKitFormBoundarygWsJMIUcbjwBPfeL");
client.UploadStringAsync(new Uri(ServerName + UriForPresetExecution), "POST", request);
}
The problem is in the client.Headers.Add method... I cannot for the sake of time figure out how HttpClient wants me to add those headers.

Please try this:
using System.Net.Http;
using System.Net.Http.Headers;
using (var httpClient = new HttpClient())
{
var httpContent = new StringContent(request);
httpContent.Headers.Clear();
httpContent.Headers.Add("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarygWsJMIUcbjwBPfeL");
var response = await httpClient.PostAsync(requestUri, httpContent);
}

Related

Call Api from WebClient() post To HttpClient() post with authentication Headers

I have this code written in deprecated WebClient and I have to update using HttpClient.
The WebClient implementation works correctly but HttpClient no, where am I doing wrong?
using (var wb = new WebClient())
{
wb.Encoding = System.Text.Encoding.UTF8;
wb.Headers.Set(HttpRequestHeader.ContentType, "application/json");
wb.Headers.Add("user_key", "USER_KEY");
wb.Headers.Add("Session_key", "SESSION_KEY");
String payload = "{" +
" \"address\": \"via xxx\", " +
" \"cell\": \"+393393939393\", "
"}";
var response = wb.UploadString("https://..../API/subaccount", "POST", payload);
// OK
}
using (HttpClient cli = new())
{
String payload = "{" +
" \"address\": \"via xxx\", " +
" \"cell\": \"+393393939393\", "
"}";
cli.DefaultRequestHeaders.Accept.Clear();
cli.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
cli.DefaultRequestHeaders.Add("Authorization", "Basic ");
cli.DefaultRequestHeaders.Add("user_key", "USER_KEY");
cli.DefaultRequestHeaders.Add("Access_token", "SESSION_KEY");
var response = cli.PostAsJsonAsync("https://..../API/subaccount", payload).Result;
//StatusCode: 400, ReasonPhrase: 'Bad Request'
}

Upload image to web API using C#

I am trying to upload an image to a web service via POST.
The API documentation says "to upload files via POST, encoded as "multipart/form-data" and include a POST arg named "image" with the image data. Images must be of type PNG, JPG, or GIF."
This is my code:
Bitmap myImage = new Bitmap("myImage.jpg");
byte[] myFileData = (byte[])(new ImageConverter()).ConvertTo(myImage, typeof(byte[]));
string myBoundary = "------------------------" + DateTime.Now.Ticks;
var newLine = Environment.NewLine;
string myContent =
"--" + myBoundary + newLine +
"content-disposition: form-data; name=\"image\"; filename=\"myImage.jpg\"" + newLine +
"Content-Type: image/jpeg" + newLine +
"Content-Transfer-Encoding: binary" + newLine +
newLine +
Encoding.Default.GetString(myFileData) + newLine +
"--" + myBoundary + "--";
try {
using (var httpClient = new HttpClient())
using (var content = new StringContent(myContent, Encoding.Default, "multipart/form-data, boundary=" + myBoundary))
using (var response = await httpClient.PostAsync("http://my_API_URL", content)) {
string responseData = await response.Content.ReadAsStringAsync();
}
}
catch (Exception myExp) { }
This code raises an exception trying to create the StringContent object.
I am ok for any suggestions. The API that I need to use requires Authentication, which typically is solved using a WebClient and this statement:
client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("my_API_key:"));
I am OK with using any other form of POST, like WebClient or HttpClient.
Code that finally worked, in case anyone is looking for the same answer:
Bitmap myImage = new Bitmap("myImage.jpg");
byte[] myFileData = (byte[])(new ImageConverter()).ConvertTo(myImage, typeof(byte[]));
string myBoundary = "---------------------------7df3c13714f0ffc";
var newLine = Environment.NewLine;
string myContent =
"--" + myBoundary + newLine +
"Content-Disposition: form-data; name=\"image\"; filename=\"myImage.jpg\"" + newLine +
"Content-Type: image/jpeg" + newLine +
newLine +
Encoding.Default.GetString(myFileData) + newLine +
"--" + myBoundary + "--";
using (var client = new WebClient()) {
try {
client.Headers["Authorization"] = "Basic xxxxxx";
client.Headers["Content-Type"] = "multipart/form-data; boundary=" + myBoundary;
client.UploadString(new Uri(myURL), "POST", myContent);
totalAPICalls++;
}
catch { }
}

C# posting data to webserver using POST method issue

While posting data I am getting issue. The issue is in the & character. I am posting a string which may contain anything. like "this&this, how are you?".
But in the above case only "this" is sending. String from the character & is stripped.
Code I tried:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://mywebsite.com/import.php");
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write("tname=sanam&temail=sanam#" + Guid.NewGuid().ToString("N") + ".com&tbody=" + this.body + "&ttitle=" + this.title);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
retStr = streamReader.ReadToEnd();
}
//retStr = "POST: " + this.body;
}
Does anybody know how to send anything to the server using C#?
Thanks in advance.
Encode the free text fields trough
System.Web.HttpUtility.UrlEncode()
IE:
streamWriter.Write("tname=sanam&temail=sanam#" + Guid.NewGuid().ToString("N") + ".com&tbody=" + HttpUtility.UrlEncode(this.body) + "&ttitle=" + HttpUtility.UrlEncode(this.title));

Exchange WebDAV Save to Drafts NOT editable

I am having the following code to Save message to Exchange WEBDAV Drafts folder. IT saves the message but If open it in Outlook, Send button is Disabled and the message is readonly. Please help me find what am i missing in this code...
thanks
Bhuvan
strBody = "To: " + strTo + "\n" +
"Subject: " + (string.IsNullOrEmpty(message.Subject) ? "" : message.Subject) + "\n" +
"Date: " + System.DateTime.Now + "\n" +
"X-Mailer: test mailer" + "\n" +
"MIME-Version: 1.0" + "\n" +
"Content-Type: text/html;" + "\n" +
"Charset = \"iso-8859-1\"" + "\n" +
"Content-Transfer-Encoding: 8bit" + "\n" +
"\n" + (string.IsNullOrEmpty(message.HtmlBody) ? "" : message.HtmlBody.Replace("<head />","").Replace("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",""));
// Create the HttpWebRequest object.
PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(tgtUri);
// Add the network credentials to the request.
// Use default credentials of the service to access the server.
PUTRequest.Credentials = cred;
// Specify the PUT method.
PUTRequest.Method = "PUT"; //PROPPATCH
// Encode the body using UTF-8.
byte[] bytes = Encoding.UTF8.GetBytes((string)strBody);
// Set the content header length. This must be
// done before writing data to the request stream.
PUTRequest.ContentLength = bytes.Length;
// Get a reference to the request stream.
PUTRequestStream = PUTRequest.GetRequestStream();
// Write the message body to the request stream.
PUTRequestStream.Write(bytes, 0, bytes.Length);
// Close the Stream object to release the connection
// for further use.
PUTRequestStream.Close();
// Set the Content-Type header to the RFC 822 message format.
PUTRequest.ContentType = "message/rfc822";
// PUT the message in the Drafts folder of the
// sender's mailbox.
PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse();
if (message.Attachments != null)
{
//Do the PROPPATCH
System.Net.HttpWebRequest PROPPATCHRequest;
System.Net.WebResponse PROPPATCHResponse;
System.IO.Stream PROPPATCHRequestStream = null;
string strxml = "<?xml version='1.0'?>" +
"<d:propertyupdate xmlns:d='DAV:'>" +
"<d:set>" +
"<d:prop>" +
"<isCollection xmlns='DAV:'>False</isCollection>" +
"</d:prop>" +
"</d:set>"
+ "<g:remove>"
+ "<g:prop><m:date/></g:prop>"
+ "</g:remove>"
+ "</d:propertyupdate>";
PROPPATCHRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(tgtUri);
PROPPATCHRequest.Credentials = cred;
PROPPATCHRequest.Headers.Set("Translate", "f");
PROPPATCHRequest.ContentType = "text/xml";
PROPPATCHRequest.ContentLength = strxml.Length;
PROPPATCHRequest.Method = "PROPPATCH";
byte[] PROPPATCHbytes = Encoding.UTF8.GetBytes(strxml);
PROPPATCHRequest.ContentLength = PROPPATCHbytes.Length;
PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream();
PROPPATCHRequestStream.Write(PROPPATCHbytes, 0, PROPPATCHbytes.Length);
PROPPATCHRequestStream.Close();
PROPPATCHResponse = (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse();
foreach (KeyValuePair<string, byte[]> attachment in message.Attachments)
{
System.IO.Stream PUTAttachRequestStream = null;
System.Net.HttpWebRequest PUTAttachRequest;
System.Net.WebResponse PUTAttachResponse;
//Attach File: This could be put in a loop to attach more than one file.
string FileName = attachment.Key; //#"2842498_794802035296_Label1.pdf";
string attachURI = tgtUri + "/" + FileName;
PUTAttachRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(attachURI);
PUTAttachRequest.Credentials = cred;
PUTAttachRequest.Method = "PUT";
PUTAttachRequest.ContentLength = attachment.Value.Length; //binaryData.Length;
PUTAttachRequestStream = PUTAttachRequest.GetRequestStream();
PUTAttachRequestStream.Write(attachment.Value, 0, attachment.Value.Length); //(binaryData, 0, binaryData.Length);
PUTAttachRequestStream.Close();
PUTAttachResponse = (System.Net.HttpWebResponse)PUTAttachRequest.GetResponse();
PUTAttachResponse.Close();
}
PROPPATCHResponse.Close();
}
// Clean up.
PUTResponse.Close();
I found the answer in Akash's blog on msdn. One has to carefully read this to get the actual answer, but the solution worked.

Has anybody implemented 2 Legged OAuth using DNOA?

I am trying to create an Authentication Module in CSharp where I need to verify the Signature from the request using DotNetOpenAuth(DNOA) Library for 2 Legged OAuth which only has consumer Key and a Secret.
If you have any sample implementation of 2 Legged OAuth using DNOA that would be helpful.
If not, any ideas on how to implement would work too.
Any help would be much appreciated.
I wasn't able to get DNOA to work with 2-legged OAuth so I ended up making my own consumer using http://oauth.googlecode.com/svn/code/csharp/OAuthBase.cs as my base class to handle the signature signing. All you need to do is subclass it and use the signature methods to build the http authorization header...
string sigMethodType = GetSigMethodType();
string ts, nonce, normalizedUrl, normalizedParams;
string sig = GenerateSignature(new Uri("http://some-endpoint-to-call"), "GET", out nonce, out ts, out normalizedUrl, out normalizedParams);
string header = "OAuth realm=\"" + normalizedUrl + "\"," +
OAuthConsumerKeyKey + "=\"" + ConsumerKey + "\"," +
OAuthSignatureMethodKey + "=\"" + "HMACSHA1SignatureType" + "\"," +
OAuthSignatureKey + "=\"" + sig + "\"," +
OAuthTimestampKey + "=\"" + ts + "\"," +
OAuthTokenKey + "=\"" + String.Empty + "\"," +
OAuthNonceKey + "=\"" + nonce + "\"," +
OAuthVersionKey + "=\"" + OAuthVersion + "\"";
Once you have the authorization header just build your web request and send it...
var wr = (HttpWebRequest)HttpWebRequest.Create(messageEndpoint.Location);
wr.Headers.Add(HttpRequestHeader.Authorization, BuildAuthHeader(messageEndpoint));
wr.ContentType = messageEndpoint.ContentType;
wr.Method = CdwHttpMethods.Verbs[messageEndpoint.HttpMethod];
using (var resp = (HttpWebResponse)req.GetResponse())
{
switch (resp.StatusCode)
{
case HttpStatusCode.Unauthorized:
Assert.Fail("OAuth authorization failed");
break;
case HttpStatusCode.OK:
using (var stream = resp.GetResponseStream())
{
using (var sr = new StreamReader(stream))
{
var respString = sr.ReadToEnd();
}
}
break;
}
}
Update:
I was also able to get 2-legged to work with devdefined's oauth consumer. http://code.google.com/p/devdefined-tools/wiki/OAuthConsumer
var endPoint = new Uri("http://example.com/restendpoint.svc");
var ctx = new OAuthConsumerContext
{
ConsumerKey = "consumerkey1",
ConsumerSecret = "consumersecret1",
SignatureMethod = SignatureMethod.HmacSha1
};
var session = new OAuthSession(ctx, endPoint, endPoint, endPoint);
var respText = session.Request().Get().ForUri(endPoint).ToString();
It would be nice if it had an empty constructor or an overload that just takes in the context, but this seems to work.

Categories