这是我要发布的请求的示例,这就是我需要在服务器上发布以上传图像的方式:
Request
POST /updateprofile HTTP/1.1
Host: <ip:port>
Content-Type: multipart/mixed, boundary=”myboundary”
User-Agent: <Device User Agent>
--myboundary
Content-Length: 2234
Delegateid=<did>&profileid=0&title=<title>& userid=<uid>&seequestion=<seequestion>&seeanswer=<seeanswer>&fname=<fname>&lname=<lname>&ccountry=<citycountry>&company=<company>&ptitle=<ptitle>&industry=<industry>&looking=<looking>&address=<address>&mobile=<mobile>&phone=<phone>&weburl=<weburl>&keys=k1|k2&values=v1111|v2222&encoding=base64&sendpic=1
--myboundary
Content-Type:image/jpeg
Content-Length: 1234
XXX Binary jpeg image xxx
--myboundary--
我对其进行了编码,但图像未上传到服务器上,每件事都已成功发布,我从图像库中获取图片并将其转换为字节数组,但仍然没有结果,我编码如下:
void _cameraCapture_Completed(object sender, PhotoResult e)
{
capturedImage = e.ChosenPhoto;
BitmapImage bmp = new BitmapImage();
bmp.SetSource(capturedImage);
MemoryStream ms = new MemoryStream();
WriteableBitmap wb = new WriteableBitmap(bmp);
wb.SaveJpeg(ms, 40, 40, 0, 40);
imageBytes = ms.ToArray();
SendPost();
}
void SendPost()
{
if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
MessageBox.Show("No network connection available!");
return;
}
//http://stackoverflow.com/questions/5952094/wp7-windows-phone-7-httpwebrequest-losing-post-data
System.Uri uri = new Uri("http://www.www.com/updateprofile.do);
// Create the web request object
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
//postData += ("&profileid=" + "0");
//postData += ("&title=" + "Mr");
//postData += ("&userid=" + "iphone@cubixlabs.com");
//postData += ("&seequestion=" + "squestion");
//postData += ("&seeanswer=" + "sanswer");
//postData += ("&fname=" + "asim");
//postData += ("&lname=" + "siddiqui");
//postData += ("&ccountry=" + "London, United Kingdom");
//postData += ("&company=" + "Google");
//postData += ("&ptitle=" + "asim");
//postData += ("&industry=" + "Transportation & Package/Freight Delivery");
//postData += ("&looking=" + "Career Opportunities");
//postData += ("&oldpasswd=" + "f30aa7a662c728b7407c54ae6bfd27d1");
//postData += ("&newpasswd=" + "f30aa7a662c728b7407c54ae6bfd27d1");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
var Response = streamReader.ReadToEnd();
//MessageBox.Show(Response.ToString());
}