By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,198 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,198 IT Pros & Developers. It's quick & easy.

Using HttpWebRequest to send POST image data

P: 4
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
Share this Question
Share on Google+
5 Replies


Plater
Expert 5K+
P: 7,872
You set the content length before building all the content
Sep 30 '08 #2

P: 4
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
Expert 5K+
P: 7,872
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

P: 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
Expert 5K+
P: 7,872
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.