Open Url sending POST - winforms

I have the this function which it has been written in html. you put a md5 value in the textbox and hit the button to start searching.
<form action="http://www.virustotal.com/vt/en/consultamd5" method="post">
<input name="hash" >
<input type="submit" value="get MD5">
My question is how do I do the something like the html function I have mentioned above, open an url, post something and see the results in the opened page?
For example in winforms put an md5 value in an textbox hit the button to start searching.

Use the HttpWebRequest as the following,
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.virustotal.com/vt/en/consultamd5");
req.Method = "POST";
Stream s = req.GetRequestStream();
StreamWriter sw = new StreamWriter(s);
sw.Write("hash=yourtexthere");
sw.Flush();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Or you can use MD5 in .net which is located in System.Cryptography.MD5 instead.

Related

ASP.Net Web Api & Angularjs: Multiple Files Upload [duplicate]

I need to upload image and video files to the server in an Angular application using Laravel 5.1 as the back end. All Ajax requests need to go to the Laravel controller first, and we have the code there for how the file gets handled when it gets there. We have previously done normal HTML forms to submit file uploads to the controller, but in this case we need to avoid the page refresh of a form, so I am attempting this in Ajax through Angular.
What information do I need to send to the Laravel controller with Ajax that was being sent to the controller via an HTML form previously?
This is the code in the Laravel controller that handled the file information once it got there. That's what I need to figure out how to send, so I can hopefully reuse this code:
$promotion = Promotion::find($id);
if (Input::hasFile('img_path')){
$path = public_path().'/images/promotion/'.$id.'/';
$file_path = $path.'promotion.png';
$delete = File::delete($file_path);
$file = Input::file('img_path');
$uploadSuccess = $file->move($path, 'promotion.png');
$promotion->img_path = '/images/promotion/'.$id.'/promotion.png';
}
if (Input::hasFile('video_path')){
$path = public_path().'/video/promotion/'.$id.'/';
$file_path = $path.'promotion.mp4';
$delete = File::delete($file_path);
$file = Input::file('video_path');
$uploadSuccess = $file->move($path, 'promotion.mp4');
$promotion->video_path = '/video/promotion/'.$id.'/promotion.mp4';
}
As you can see above, we are converting whatever file we get to a PNG with the file name promotion.png so it's easy to fetch, and we are only accepting .mp4 video format. Because of that, we don't need to worry about checking if the file exists and is it ok to overwrite it. That's why you can see in the code we delete any existing file of that name before saving.
The HTML was just an input with a type of "file:
<input type="file" id="img_path" name="img_path" class="promo-img-path" accept="image/*">
We are using Angular now so I can't just send the above through an HTML form anymore. That's what I need to figure out how to do.
We are two developers just doing our best, so I'm sure there is a better way of doing this. However before I refactor this whole thing, I'm hoping I can use Angular (or jQuery as a last resort) to just send the controller whatever file data Laravel needs in order to make the above code work. The answer may be as simple as "send a PUT to the method in that controller above, but instead of a normal JSON payload, use file info in this format and you can gather that info with..."
I would also appreciate any tips on better ways I can do this in the future.
How to POST FormData Using the $http Service
When using the FormData API to POST files and data, it is important to set the Content-Type header to undefined.
var fd = new FormData()
for (var i in $scope.files) {
fd.append("fileToUpload", $scope.files[i]);
}
var config = {headers: {'Content-Type': undefined}};
var httpPromise = $http.post(url, fd, config);
By default the AngularJS framework uses content type application/json. By setting Content-Type: undefined, the AngularJS framework omits the content type header allowing the XHR API to set the content type. When sending a FormData object, the XHR API sets the content type to multipart/form-data with the proper boundaries and base64 encoding.
For more information, see MDN Web API Reference - XHR Send method
How did you get the file information into $scope.files?
How to enable <input type="file"> to work with ng-model
This directive also enables <input type="file"> to automatically work with the ng-change and ng-form directives.
angular.module("app",[]);
angular.module("app").directive("selectFilesNg", function() {
return {
require: "ngModel",
link: function postLink(scope,elem,attrs,ngModel) {
elem.on("change", function(e) {
var files = elem[0].files;
ngModel.$setViewValue(files);
})
}
}
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
<h1>AngularJS Input `type=file` Demo</h1>
<input type="file" select-files-ng ng-model="fileArray" multiple>
<code><table ng-show="fileArray.length">
<tr><td>Name</td><td>Date</td><td>Size</td><td>Type</td><tr>
<tr ng-repeat="file in fileArray">
<td>{{file.name}}</td>
<td>{{file.lastModified | date : 'MMMdd,yyyy'}}</td>
<td>{{file.size}}</td>
<td>{{file.type}}</td>
</tr>
</table></code>
</body>
RECOMMENDED: POST Binary Files Directly
Posting binary files with multi-part/form-data is inefficient as the base64 encoding adds an extra 33% overhead. If the server API accepts POSTs with binary data, post the file directly.
See How to POST binary files with AngularJS (with DEMO)

"Please use multipart." When uploading to upload URL from google app engine

I obtained a upload uRL Programmatically from my deployed google app., however i keep getting the below response...what type of parameter am i missing?
Bad content type. Please use multipart.
2016-08-16 09:25:34 ERROR PayitWebClientApp:207 - 400 Bad Request
Bad content type. Please use multipart.
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1054)
at com.vanitysoft.payit.PayitInvokeAuthApp.generateUploadUr(PayitInvokeAuthApp.java:198)
at com.vanitysoft.payit.PayitInvokeAuthApp.main(PayitInvokeAuthApp.java:217)
This is the code:
MultipartContent content = new MultipartContent().setMediaType(
new HttpMediaType("multipart/form-data")
.setParameter("boundary", "__END_OF_PART__"));
FileContent fileContent = new FileContent(
"multipart/related", file);
MultipartContent.Part part = new MultipartContent.Part(fileContent);
part.setHeaders(new HttpHeaders().set(
"Content-Disposition",
String.format("form-data; name=\"content\"; filename=\"%s\"", file.getName())));
content.addPart(part);
httpRequest = httpRequestFactory.buildPostRequest(new GenericUrl(uploadItem.getUploadUrl()) ,fileContent);
LOGGER.info("upload...[" + uploadItem.getUploadUrl() +"]");
httpResponse = httpRequest.execute();
LOGGER.info("Upload complete");
Assert.isTrue(httpResponse.getStatusCode() == 200, IOUtils.toString(httpResponse.getContent()));
LOGGER.info( "Success[" + httpResponse.getContent() +"]" );
You are missing enctype="multipart/form-data" on <form> tag.
Check this answer: Why is form enctype=multipart/form-data required when uploading a file?
Opps, typo:, i needed to use 'content' not fileContent
httpRequest = httpRequestFactory.buildPostRequest(new GenericUrl(uploadItem.getUploadUrl()) ,content);

Hidden iFrame for MultiPartFileUpload issue in FireFox Browser

My framework is Spring-MVC with AngularJS. For File Upload i am using Multipart File Upload. I created a hidden iFrame to submit file to backend. The code looks like this.
function fnUploadFile(formData){
var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none"></iframe>');
$("body").append(iframe);
var form = $('#uploadform');
form.attr("action", CONTEXT_PATH+"admin/uploadPDF");
form.attr("method", "post");
form.attr("encoding", "multipart/form-data");
form.attr("enctype", "multipart/form-data");
form.attr("target", "postiframe");
form.attr("uploadfile", $('#uploadfile').val());
var input = $('<input type="hidden" name="filename"/>').val(formData);
input.appendTo(form);
form.submit();
}
After that I save the file in server location.I am having problem while uploading file from FireFox browser. Its opening a new pop-up right after we click upload button.
Can someone help resolving this issue?
Found the solution for it by myself. Just added text/plain as header in HttpServletResponse. It resolves my problem.

Uploading XML file to the Google App Engine DataStore

I am looking for the best way to upload the XML file to the GAE DataStore from the web page. The XML will be later parsed and modified wia the web interface. So far I am using the HTMLform with file typeinput:
<form enctype="multipart/form-data" action="update" method="post" >
<input type="file" name="myfile" />
<input type="submit" />
</form>
In the servlet class I use the for loop to read the data into String:
InputStream input = req.getInputStream();
StringBuffer sb = new StringBuffer("");
int c = -1;
while ( (c = input.read() ) != -1 )
{
char ch = (char) c;
sb.append( ch );
}
Then I check if the DataStore contains the entity with application hardcoded key value and if not I create a new entity and upload the XML into Text (com.google.appengine.api.datastore.Text), otherwise I create a new entity and put the file there. Is that something you can call the good approach?
Regards,
STeN
If you use a html form with type="file" field, then browser will upload the file via http POST with a multipart/form-data content type.
See AppEngine docs on how to handle properly multipart content data.

Uploading a file with YUI3 to a RESTful PUT url

I'm trying to upload a file to a RESTful PUT url with YUI3, but when I set upload to true in the config to io it sends the file as POST not PUT. If I remove the upload setting in the config I just get the filename, but it does go to the PUT url. Can I use PUT with a file upload? Is there another way to do this?
I'm assuming this is a failure/fault in YUI3 or rather my use of it.
Form:
<form id='GFileForm' method='PUT' onSubmit='return false;'>
<input type='file' name='gfile' id='GFileName'>
<input type='submit' name='gfileupload' value='Upload' id='GFileUpload_Button'>
</form>
JS:
var cfg = {
method: "PUT",
form: {id: 'GFileForm', upload: true},
content_type: "multipart/form-data",
};
var request = Y.io(sUrl, cfg);
Any help is here much appreciated.
I've also tried to find a resource on reading the file contents with javascript and then pushing that into the PUT data, but I can't seem to find anything about that. Does anyone know if that's a possiblilty?
Cheers,
Andy.
PUT is not a standard way of sending form data and most web browsers don't support it, unfortunately.
Check this example. https://github.com/chmouel/cors-swift-example
In this example, you can see how we can PUT files to RESTful PUT url.

Resources