各位大神,小弟今天对安卓上很火的360移动搜索拍题做一个post提交图片的过程,很是郁闷post后一直返回的html为空,下面是我的代码希望有人可以给我指点迷津
private void button2_Click(object sender, EventArgs e)
{
HttpUploadFile("http://xueba.onebox.so.com/ocr/api", @"D:\sj-img\webwxgetmsgimg.jpg");
}
public string HttpUploadFile(string url, string filepath)
{
string boundary = "OCRFormBoundaryaqAZrhGGayPmw2s3";
// 创建request对象
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.ContentType = "multipart/form-data; boundary=---" + boundary;
webrequest.Method = "POST";
//webrequest.Referer = refre;
webrequest.Accept = "*/*";
webrequest.KeepAlive = true;
webrequest.Headers.Add("Accept-Encoding", "identity");
webrequest.UserAgent = "Dalvik/1.6.0 (Linux; U; Android 4.0.3; T9200 Build/HuaweiT9200)";
FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
// 构造发送数据
StringBuilder sb1 = new StringBuilder();
// 文件域的数据
sb1.Append("-----" + boundary);
sb1.Append("/r/n");
sb1.Append("Content-Disposition: form-data; name=\"image\";filename=\"XuebaHelp_Camera_Crop.jpg\"");
sb1.Append("/r/n");
sb1.Append("Content-Type: ");
sb1.Append("application/octet-stream(" + fileStream.Length + "bytes)");
sb1.Append("/r/n/r/n");
string postHeader = sb1.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
StringBuilder sb2 = new StringBuilder();
string a = "APP: 1.3.0, Density: 1.5, DensityDpi: 240, ScaledDensity: 1.5, WidthPixels: 960, HeightPixels: 540, XDPI: 152.4, YDPI: 451.55554, Product: T9200, CPU_ABI: armeabi-v7a, TAGS: ota-rel-keys,release-keys, VERSION_CODES.BASE: 1, MODEL: T9200, SDK: 15, VERSION.RELEASE: 4.0.3, DEVICE: hwt9200, DISPLAY: HUAWEI T9200 V100R001CHNC01B218, BRAND: Huawei, BOARD: T9200, ID: HuaweiT9200, MANUFACTURER: HUAWEI, USER: huawei, Mid: e049d5b36a1fcd858e153f1ee1fc2116, NetworkType: 8, PhoneType: 1, OperatorName: 中国移动, Wifi: 1, Intversion: 2, AppChannel: mso_app";
sb2.Append("\r\n");
sb2.Append("-----");
sb2.Append(boundary);
sb2.Append("\r\n");
sb2.Append("Content-Disposition: form-data; name=\"src\"Data:onebox");
sb2.Append("\r\n");
sb2.Append("-----");
sb2.Append(boundary);
sb2.Append("Content-Disposition: form-data; name=\"unique\"");
sb2.Append("Data:");
sb2.Append(a);
sb2.Append("\r\n");
sb2.Append("-----");
sb2.Append(boundary);
sb2.Append("--\r\n");
//构造尾部数据
byte[] boundaryBytes = Encoding.ASCII.GetBytes(sb2.ToString());
Stream mestream = new MemoryStream();
long length = postHeaderBytes.Length + fileStream.Length+boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
// 输入头部数据
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
// 输入文件流数据
byte[] buffer = new Byte[(int)fileStream.Length];
// byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
// 输入尾部数据
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
requestStream.Close();
Thread.Sleep(500);
WebResponse responce = webrequest.GetResponse();
Stream s = responce.GetResponseStream();
StreamReader sr = new StreamReader(s);
string str = sr.ReadToEnd();
MessageBox.Show(str);
return str;
}