 |
|
|
Could this be modified to do a post that requires some POST strings, the file, but then more strings afterwards? I have a website with a very funky post that request 2 string, a file, then 4 more strings. I can't figure out how to do this. I've look at dozens of great samples, like the one above, but can't find how to this what that funky website wants.
Cheers, Glenn
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi thanks for the article.it helps me big time.I need a bit more help in this regard. you are using query string to send form fields..Since the form i want to simulate has too many fields so i cant send them as query string. So i have to add both form fields and upload file in the request stream.Please help me out to make body part of request stream Many thanks
wasif
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello, first is an wonderful code snipped. but i have a problem, I need to post 5 files and not only one. Can anyone help me how does it work?
My code:
sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append(fileFormName); sb.Append("\"; filename=\""); //sb.Append(Path.GetFileName(uploadfile)); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append(contenttype); sb.Append("\r\n"); sb.Append("\r\n");
/// sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append(fileFormName); sb.Append("\"; filename=\""); sb.Append(Path.GetFileName(uploadfile)); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append(contenttype); sb.Append("\r\n"); sb.Append("\r\n");
string postHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
Felix
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
de verdad esta solucion es muy util para realizar muchos procesos de envio via web se agradece al autor!! saludos kedoz
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Here's small patch. Aside from that great job, thank you! BEFORE: while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) requestStream.Write(buffer, 0, bytesRead);
// Write out the trailing boundary AFTER: while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) requestStream.Write(buffer, 0, bytesRead); fileStream.Close(); // ! ADDED // Write out the trailing boundary
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I want to be able to decode a multipart post with out using a PHP or .ASPX page.
Any one know of any reference material or sample projects?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello... I've been playing around with the code for a while trying to get it to retrieve a responses the same way as a web browser, by that I mean, waiting for responses and retrieving them in the same way a web browser would... is there any way to do this?
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
I have tried changing the timeout with no luck. It will just hangs there forever.
This class may work fine for 2 hours and suddenly have this problem.
Please help...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi when I try to upload a file, I always get a Webexception at 1:40 minutes at this code: "requestStream.Write(buffer, 0, bytesRead);" I changed the Buffer size and tried it with different files, but the bug is always the same
Can you tell me why this bug appears and can you fix it?
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hello,
The UploadFileEx works fine for the first file I have ti upload but the Web server is waiting for another file a attachment (Thy talk about DIME). Somebody could tell me how I could ride on it whit this solution ?
Tnahk's a lot for help
PhBV
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
The C# code works fine but the sr.ReadToEnd() return this: Parse error: syntax error, unexpected T_STRING in C:\php\upload2.php on line 10
I'm very unexperienced with PHP. Anybody an idea how I can solve this?
Thanks!
Pete
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I'm using the UploadFileEx, post version, and get an exception in Tomcat. Stream ended unexpectedly. I see that the length is ok, I send all packets, but, seems server don't get it. I use the 8080 port.
Any idea?
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
when i post my file to the webserver (https) i am getting an error that i cannot resolve.
Error: Cannot find bean: "Uploadbean" in any scope
any ideas on what would cause this error?
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
We had problems sending files larger then 7 Mb or so. The HttpWebRequest has a Timeout property, which defaults to 100000 ms, we upped that and it now seems to be working for larger files.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
For some reason, the component would not work for me. I did find a more flexible solution, in the form of a cheap .NET component that allows you to navigate to pages, and interact with forms. It allows for multipart form post, and file upload, handling cookies and redirects and proxies automatically. It's pretty nifty.
You can find it over at http://foxtrot-xray.com/web-robot
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
it didnt work for me. i get a weird error from the webpage that i am sending the file to. something about UploadBean
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
i have been try to login on http://www.codeproject.com through C# code which i have attached with this mail. problem is this that code executes fine but results r not fine. every time it gives me start page as a response, not my page but start page. if someone be kind enough to rectify the problem and send the corrected code back to me. Code is here. as space is limited so iam posting it in 2 part. 1st part is below but the second one will be a reply to this message.
public void SendRequestTo() { string UserName; string UserPwd; string Request; string Cookie=""; string RequestMethod="POST"; string uri="http://www.codeproject.com" // Read UserName and User Password from the user in UserName and UserPwd respectively Request="Email="+UserName+"&PassWord="+UserPwd;
NameValueCollection collHeader = new NameValueCollection(); HttpWebResponse webresponse; try { HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri); webrequest.KeepAlive = true; webrequest.Timeout=180000; webrequest.Method = RequestMethod; webrequest.Accept = "*/*"; webrequest.AllowAutoRedirect = false; webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; " + "Windows NT 5.0;SV1;.NET CLR 1.1.4322)"; webrequest.ContentLength = Request.Length; webrequest.ContentType = "text/html"; //"application/x-www-form-urlencoded"; int iCount = collHeader.Count; string key; string keyvalue; for (int i=0; i < iCount; i++) { key = collHeader.Keys[i]; keyvalue = collHeader[i]; webrequest.Headers.Add(key, keyvalue); } CredentialCache wrCache = new CredentialCache(); wrCache.Add(new Uri(uri),"Basic",new NetworkCredential(UserName,UserPwd)); webrequest.Credentials = wrCache; //Remove collection elements collHeader.Clear(); webresponse =(HttpWebResponse)webrequest.GetResponse(); string uri2="";
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
WebHeaderCollection headers = webresponse.Headers; if ((webresponse.StatusCode == HttpStatusCode.Found) || (webresponse.StatusCode == HttpStatusCode.Redirect) || (webresponse.StatusCode == HttpStatusCode.Moved) || (webresponse.StatusCode == HttpStatusCode.MovedPermanently)) { // Get redirected uri uri2 = headers["Location"] ; uri2 = uri2.Trim(); } //Check for any cookies if (headers["Set-Cookie"] != null) {Cookie = headers["Set-Cookie"]; } string ReUri =uri2 //Check if there is any redirected URI. webresponse.Close(); ReUri = ReUri.Trim(); if (ReUri.Length == 0) //No redirection URI {ReUri = uri; } NameValueCollection collHeader = new NameValueCollection(); if (Cookie.Length > 0) {collHeader.Add("Cookie",Cookie); } webrequest=null; webrequest = (HttpWebRequest)WebRequest.Create(ReUri); webrequest.KeepAlive = true; webrequest.Timeout=180000; webrequest.Method = RequestMethod; webrequest.Accept = "*/*"; webrequest.AllowAutoRedirect = false; webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; " + "Windows NT 5.0;SV1;.NET CLR 1.1.4322)"; webrequest.ContentLength = Request.Length; webrequest.ContentType = "text/html"; //"application/x-www-form-urlencoded"; int iCount2 = collHeader.Count; string key2; string keyvalue2; for (int i2=0; i2 < iCount2; i2++) { key2 = collHeader.Keys[i2]; keyvalue2 = collHeader[i2]; webrequest.Headers.Add(key2, keyvalue2); } webrequest.Credentials = wrCache; BuildReqStream(ref webrequest); webresponse=null; webresponse = (HttpWebResponse)webrequest.GetResponse(); Encoding enc = System.Text.Encoding.GetEncoding(1252); StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(),enc); string Response = loResponseStream.ReadToEnd(); loResponseStream.Close(); webresponse.Close(); }//End of Try Block catch (WebException e) {System.Windows.Forms.Show(e.toString()); } catch (System.Exception e) {System.Windows.Forms.Show(e.toString()); } finally {BaseHttp=null; } System.IO.FileStream strr=new FileStream("C:\\itsnew.html",FileMode.Append); byte[] bt=Encoding.GetEncoding(1252).GetBytes(Response); strr.Write(bt,0,bt.Length); } //End of SendRequestTo method
private void BuildReqStream(ref HttpWebRequest webrequest) //This method build the request stream for WebRequest { byte[] bytes = Encoding.ASCII.GetBytes(Request); System.Windows.Forms.MessageBox.Show(Request); webrequest.ContentLength=bytes.Length; Stream oStreamOut = webrequest.GetRequestStream(); oStreamOut.Write(bytes,0,bytes.Length); oStreamOut.Close(); }//End BuildReqStream method
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks for the code...  But i am getting a problem right exactly while postinng. I think its related to the request header...
Following code i have used in my application :-
private void cmdUpload_Click(object sender, System.EventArgs e) { string sURI = "http://adityamooley.net/photos/api/index.php"; CookieContainer cookies = new CookieContainer(); //add or use cookies NameValueCollection querystring = new NameValueCollection(); querystring["aid"]="7"; querystring["username"]="aditya"; querystring["password"]="aditya"; querystring["function"]="cpgAPI.upload"; querystring["title"]="Title"; querystring["description"]="description"; querystring["keywords"]="keywords"; string uploadfile;// set to file to upload uploadfile = @"e:\rahul\test.jpg";
//everything except upload file and url can be left blank if needed string outdata = UploadFileEx(uploadfile, sURI,"uploadfile", "image/pjpeg", querystring,cookies); }
the function code i didn't change. at the statement :
WebResponse responce = webrequest.GetResponse();
i get the above mentioned error: (406) Not Acceptable.
If anyone have any idea how to solve it, it will be a great help.
-- Regards, Rahul.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
It is possible with the .NET 2.0 Framework, as it makes the WebClient a bit more flexible.
public class WebClientExtended : WebClient { private CookieContainer myContainer; private HttpWebRequest myRequest; private string myMethod;
public string Method { get { return myMethod; } set { myMethod = value; } }
public CookieContainer Cookies { get { if (myContainer == null) { myContainer = new CookieContainer(); } return myContainer; } set { myContainer = value; } }
protected override WebRequest GetWebRequest(Uri address) { myRequest = (HttpWebRequest)base.GetWebRequest(address); myRequest.Method = this.Method; myRequest.CookieContainer = Cookies; return myRequest; }
protected override WebResponse GetWebResponse(WebRequest request) { return myRequest.GetResponse(); }
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result) { return myRequest.EndGetResponse(result); } }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It's a nice idea, but you should not set CookieContainer unless you're sure that base.GetWebRequest is an HttpWebRequest.
protected override WebRequest GetWebRequest(Uri address) { WebRequest wRequest = base.GetWebRequest(address);
if (wRequest is HttpWebRequest) (wRequest as HttpWebRequest).CookieContainer = myContainer;
return wRequest; }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |