我正在尝试使用网络服务发送图片。我首先将图像转换为base64字符串并使用Web请求发送到服务器的Post方法。这是我的代码。
try
{
qry = "Select ProductImage From tbl_Product where ProductOnlineID=0";
dt = db.DataTable(qry);
foreach (DataRow dr in dt.Rows)
{
string fileName = dr["ProductImage"].ToString();
string storeLocation = Application.StartupPath + @"\product_image\";
if (System.IO.File.Exists(storeLocation + fileName))
{
Image image = Image.FromFile(storeLocation + fileName);
MemoryStream m = new MemoryStream();
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
string url_upload_imag = @"http://Myserver/api/offline/json/1.0/index.php";
string rq = "token=" + apikey + @"&json_data={%22method%22:%22uploadProductImagesLocal%22,%22data%22:[{%22imgName%22:%22" + fileName + "%22,%22imgString%22:%22" + base64String + "%22}]}";
WebRequest request = WebRequest.Create(url_upload_imag);
request.Method = "POST";
request.ContentLength = rq.Length;
byte[] array = new UTF8Encoding().GetBytes(rq);
request.ContentType = "application/x-www-form-urlencoded";
using (Stream str = request.GetRequestStream())
{
str.Write(array, 0, array.Length);
using (WebResponse ws = request.GetResponse())
{
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject_Image));
RootObject_Image obj = (RootObject_Image)jsonSerializer.ReadObject(ws.GetResponseStream());
MessageBox.Show("Done" + fileName + obj.data);
}
}
}
}
}
catch (WebException ex)
{
MessageBox.Show("Error in Sending Image:" + ex.ToString(), "Error:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
我的问题是..当我运行应用程序时,我得到超时错误..我已经尝试了将超时设置为无限的一切对我不起作用。我的base64字符串也是正确的。
Web服务是在php中创建的。什么可能是错误?如果有任何错误,可以是PHP或C#?