Looping through array in PHP to post several multipart form-data - asp.net

I'm trying in an asp web application to code a function that would loop through a list of files in a multiple upload form and send them one by one.
Is this something that can be done in ASP? Because I've read some posts about how to attach several files together, but saw nothing about looping through the files. I can easily imagine it in C# via HttpWebRequest or with socket, but in php, I guess there are already function designed to handle it?
// This is false/pseudo-code :)
for (int index = 0; index < number_of_files; index++)
{
postfile(file[index]);
}
And in each iteration, it should send a multipart form-data POST.
postfile(TheFileInfos) should make a POST like it:
POST /afs.aspx?fn=upload HTTP/1.1
[Header stuff]
Content-Type: multipart/form-data; boundary=----------Ef1Ef1cH2Ij5GI3ae0gL6KM7GI3GI3
[Header stuff]
------------Ef1Ef1cH2Ij5GI3ae0gL6KM7GI3GI3
Content-Disposition: form-data; name="Filename" myimage1.png
------------Ef1Ef1cH2Ij5GI3ae0gL6KM7GI3GI3
Content-Disposition: form-data; name="fileid"
58e21ede4ead43a5201206101806420000007667212251
------------Ef1Ef1cH2Ij5GI3ae0gL6KM7GI3GI3
Content-Disposition: form-data; name="Filedata"; filename="myimage1.png"
Content-Type: application/octet-stream
[Octet Stream]
[Edit]
I'll try it:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action="processFiles.php">
<p>
<?
// start of dynamic form
$uploadNeed = $_POST['uploadNeed'];
for($x=0;$x<$uploadNeed;$x++){
?>
<input name="uploadFile<? echo $x;?>" type="file" id="uploadFile<? echo $x;?>">
</p>
<?
// end of for loop
}
?>
<p><input name="uploadNeed" type="hidden" value="<? echo $uploadNeed;?>">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

Related

Spring MVC Redirect with POST

I am trying to redirect a user to external url to complete the transaction and this website is based SAML based authentication and accepts SAMLResponse as a post parameter. Below is my controller able to get the values in the logs
----- Controller ---------------------------------------------------
#RequestMapping(value="/redirect", method = { RequestMethod.GET, RequestMethod.POST })
public String redirect(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttrs) {
String samlRes = String.valueOf(request.getSession().getAttribute("STRRESPONSE"));
byte[] base64samlRes = Base64.encode(samlRes.getBytes());
redirectAttrs.addAttribute("SAMLResponse", base64samlRes);
L.debug("Redirecting " + samlRes);
L.debug("Redirecting Base64 Encoder " + new String(base64samlRes, Charset.forName("UTF-8")));
return "redirect";
---------------- Controller Ends ----------------------
and using the below html to post the external website.. I am seeing the browser the html title but it is not loading the external portal and values in the html is also null
---- HTML auto submit -----------------------
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Redirect Payment Portal</title>
</head>
<body>
<ul>
<li>SamlResponse: "${SAMLResponse}"</li>
</ul>
<form name="myRedirectForm" action="<external url>" method="post">
<input name="SAMLResponse" type="hidden" value="${SAMLResponse}" />
<noscript>
<input type="submit" value="Click here to continue" />
</noscript>
</form>
<script type="text/javascript">
$(document).ready(function() {
document.myRedirectForm.submit();
});
</script>
</body>
</html>
===================== End HTML ----------------------
Am i missing anything
I am able to retrieve the value in html by modifying the value [[${SAMLResponse}]] but the form is not getting submitted
Finally issue is resolved with the following updates
------ HTML---
modified the following line
<input name="SAMLResponse" type="hidden" value="${SAMLResponse}" />
to
<input name="SAMLResponse" type="hidden" th:value="${SAMLResponse}" />
--- End HTML ----
----- Controller Changes ----
#RequestMapping(value="/redirect", method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView redirect(HttpServletRequest request, HttpServletResponse response, Model model) {
String samlRes = String.valueOf(request.getSession().getAttribute("STRRESPONSE"));
byte[] base64samlRes = Base64.encode(samlRes.getBytes());
model.addAttribute("SAMLResponse", new String(base64samlRes, Charset.forName("UTF-8"));
L.debug("Redirecting " + samlRes);
L.debug("Redirecting Base64 Encoder " + new String(base64samlRes, Charset.forName("UTF-8")));
return new ModelAndView ("redirect");
---- Controller Changes End ------

Unable to serve static resources with spring, receive index instead

I have a simple Spring web application.
#Controller
public class GreetingController {
#Autowired
GreetingRepository repository;
#GetMapping("/greeting")
public String greetingForm(Model model) {
model.addAttribute("greeting", new Greeting());
return "greeting";
}
#PostMapping("/greeting")
public String greetingSubmit(#ModelAttribute Greeting greeting) {
repository.save(greeting);
return "result";
}
}
The HTML is:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Greeting Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" th:href="#{/css/style.css}"/>
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="#{/greeting}" th:object="${greeting}" method="post">
<p>Id: <input type="text" th:field="*{id}" /></p>
<p>Message: <input type="text" th:field="*{content}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
This is the css file that is referenced in the HTML:
p {
color: #ff5588;
}
However, the css is not applied.
In fact, when I query localhost:8080/css/style.css it returns the HTML content of the index page.
It might be a security configuration issue. Is there a security implementation (SpringSecurity or custom) on your project? If so, does your user have access to /css/* url or this url has public access? The redirection might be occur if user doesn't have access to url or there is an exception while processing request.
The security configuration turned out to be blocking all requests to resources. In stead, it was redirecting those requests to the index page (which was configured).
The solution turned out to be simple, authorize all requests to the resources of the website (/images/** and /css/** and /js/**).
Once that was configured, the web-application worked as expected.

Automate HTTPS Login

I'm looking at a login page, and I need to create a POST message for it. Here is the login page source.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<link rel="stylesheet" href="style.css" />
<script language="javascript" src="jquery.js"></script>
<script type="text/javascript" language="javascript">$(document).ready(function(){});</script>
</head>
<body style="background-color:#FFF; background-image:url(satellite.png); background-repeat:no-repeat; background-position:top center; max-width:100%;">
<div id="login" >
<form id="auth_user_sys" name="auth_user_sys" action="index.php" method="post">
<fieldset>
<legend>Authentication</legend>
<table>
<tr><td colspan="2"> </td></tr>
<tr><td><label>* Username </label></td><td><input id="user_sys" name="user_sys" type="text"/></td></tr>
<tr><td><label>* Password </label></td><td><input id="user_p_sys" name="user_p_sys" type="password"/></td></tr>
<tr><td colspan="2" align="center"><input id="btlog" name="btlog" type="submit" value="Connection"/></td></tr>
<tr><td colspan="2"></td></tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>
I've tried and failed so far (I'm using Qt):
QUrlQuery urlQuery;
urlQuery.addQueryItem("user_sys", username);
urlQuery.addQueryItem("user_p_sys", password);
QString params = urlQuery.query();
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setHeader(QNetworkRequest::ContentLengthHeader, params.length());
pNetMgr->post(request, params.toLatin1());
I say it fails because the reply to the POST is the same as the reply to the original GET for that URL -- namely the source for the login page.
First, QUrlQuery takes care of encoding itself:
QString username = "aba";
QString password = "equals-sign:=amp:&nice";
QUrlQuery urlQuery;
urlQuery.addQueryItem("user_sys", username);
urlQuery.addQueryItem("user_p_sys", password);
QString params = urlQuery.query();
qDebug() << params;
displays:
user_sys=aba&user_p_sys=equals-sign:%3Damp:%26nice
I am wondering what types username and password have in your code because toPercentEncoding() does only exist for QByteArray and QUrl. You need QStrings.
Secondly, params.toLatin1() already returns a QByteArray. It makes no sense to convert it into an unhandy C-style-pointer and from there back into a QByteArray. Go for
pNetMgr->post(request, params.toLatin1());
Thirdly, you are sending latin1 encoded data. Does you username or assword contain characters, that are not in the latin1 alphabet?
I figured it out. In the Authentication section there are 3 "input names", but I was only sending 2. I added the "btlog" item (see below), and it came back with a set-cookie and a link to the page I needed to go to.
QUrlQuery urlQuery;
urlQuery.addQueryItem("btlog", "Connection");
urlQuery.addQueryItem("user_sys", username);
urlQuery.addQueryItem("user_p_sys", password);
QString params = urlQuery.query();
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
request.setHeader(QNetworkRequest::ContentLengthHeader, params.length());
pNetMgr->post(request, params.toLatin1());

Why does this var REQUEST ["name"] method always return null?

I'm self-learning how to create a form, pass the responses to a separate handler, and email them to my website account. I have the form, which works, and oddly enough, I have the mail portion working. However, this example is almost straight out of some ASP.NET help pages at Microsoft - yet it does not work, and I can't seem to find out why. What's wrong and how should it read instead?
Here's some form code:
{
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="Mailform.cshtml" enctype="text/plain"
method="post" name="formcontent">
<ul>
<li>
<label for="name" class="heavyred">Contact Name: &nbsp</label>
<input type="text" name="contact" autofocus="true" size="40"
required="" /><span class="tinyfont">&nbsp red denotes a
required field</span>
<span class="italicfont"style="margin-left: 22%"><br>Person
overseeing site development</span>
</li>
<li>
<label for="strngweak">List strengths:<br></label>
<textarea name="strngweak" placeholder="420 chars max"
cols="60" rows="7"></textarea>
</li>
</ul>
<div>
<input class="hyper buttn" style="margin-right:1%; font-size: 1em;
"type="submit" value="SUBMIT">
</div>
</form>
</body>
}
handler.cshtml code: (the first two requests end up NULL instead of getting data by name)
{
#{
var contact = Request ["contact"]; <----- NULL instead of name=contact data
var stweak = Request ["strngweak"]; <----- NULL instead of name=strngweak data
var from = "info#portalmagician.com";
var to = "sirrobcop#yahoo.com";
var subject = "I WANT A WEBSITE";
var msg = "From: ";
var errorMessage = "";
var debuggingFlag = true;
msg += contact + " Strengths: " + stweak;
}
{ try-catch block initializes webmail helper and sends email here }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
#if (IsPost) {
if (errorMessage == "") {<p>Your message has been sent!</p>;}
else {<p>Unable to send due server down or bad server info.</p>;}
}
</body>
}
Modify the enctype of your form to application/x-www-form-urlencoded (or delete it).

The remote server returned an error: (405) Method Not Allowed. c#

So I am working in a project to send data from a C# application to ASP web application; the problem is when I want to send the data I get this error :The remote server returned an error: (405) Method Not Allowed. Here is my C# code:
static void Main(string[] args)
{
using (var wb = new WebClient())
{
var data = new NameValueCollection();
string url = "http://localhost:4241/HtmlPage2.html";
data["Text1"] = "Anas";
data["Text2"] = "Anas";
var response = wb.UploadValues(url, "POST", data);
}
}
and here my ASP code (it's just a HTML page for testing, the code is implemented in HtmlPage2.html and when I submit the button it passes the data to HtmlPage2.html)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form method="Post" action="HtmlPage2.html">
<input id="Text1" type="text" name="Text1" />
<input id="Text2" type="text" name="Text2" />
<input id="Button1" type="submit" value="button" />
</form>
</body>
</html>
NB:My IIS is already enabled :)
So if someone can help me I will be very appreciative :)
A HTML file is a static file on a filesystem-based webserver, there is no way for it to handle POST requests, only GET (unless you're using WebDAV, but that's another story).
Basically the <form action="HtmlPage2.html"> fragment is incorrect. If it does work in your webbrowser then check the DOM of the page to see if the action attribute is being changed.

Resources