469,568 Members | 1,391 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

Using HttpWebRequest to send POST image data

Hello, I am trying to use HttpWebRequest to send some POST
data
I have accomplished this using:

HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("http://mysite.com/index.php");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "login=value1&pwd=value2&file=filename"
req.ContentLength = postData.Length;

StreamWriter stOut = new
StreamWriter(req.GetRequestStream(),
System.Text.Encoding.ASCII);
stOut.Write(postData);
stOut.Close();

but i am getting response Upload problem but when i am uploading file using html Form-based File Upload then file is uploading it is showing upload success.
Sep 30 '08 #1
5 8013
Plater
7,872 Expert 4TB
You set the content length before building all the content
Sep 30 '08 #2
hi i am uploading image by this method but i am getting upload problem response.plz anyone give me solution.

private void postImage()
{
HttpWebRequest request ;
request = (HttpWebRequest)HttpWebRequest.Create("url");




string strfileName = FileUpload1.PostedFile.FileName;


string strLogin = "login="+txtLogin.Text;
string strPassword = "&pass=" + txtPassword.Text;
string strFile1 = @"&file="+ strfileName;

string strRequestMessage = strLogin+strPassword+strFile1;

request.Method = "POST";
request.ContentLength = strRequestMessage.Length ;
request.ContentType = "application/x-www-form-urlencoded";

request.KeepAlive = true;

//Create StreamWriter object
StreamWriter objStreamWriter = new StreamWriter(request.GetRequestStream());


objStreamWriter.Write(strRequestMessage);

objStreamWriter.Close();
//Get the response
System.Net.HttpWebResponse objWebResponse = (HttpWebResponse)request.GetResponse();
System.IO.StreamReader objStreamReader = new System.IO.StreamReader(objWebResponse.GetResponseS tream());
string strResponse = objStreamReader.ReadToEnd().Trim();
objStreamReader.Close();


}
Sep 30 '08 #3
Plater
7,872 Expert 4TB
THREADS MERGED.
Please do not double post your questions. It is against the posting guidelines.

MODERATOR



Now then, Where exactly are you reading in the contents of the file you wish to upload and sending that? I do not see it in the code provided, it appears you do not do it at all.
Sep 30 '08 #4
i am sending name of the file in strFileName. Do i need to send byte array of the file
Sep 30 '08 #5
Plater
7,872 Expert 4TB
Yes, that is how it works. You need do some kinda of content disposition heading too I think which could be tricky.
I recomend searching for a quick example on the web, there should be plenty.
Sep 30 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by supster | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.