前台代码如下:<form method="post" action="http://xxxxx.xxxxx.com/api/xxxxx.php" enctype="multipart/form-data">
EMAIL<input type="text" name="EMAIL" />
PASSWORD<input type="text" name="PASSWORD"/>
TEXT<input type="text" name="TEXT"/>
</br>
PLACE_ID<input type="text" name="PLACE_ID"/>
PHOTO<input type="file" name="PHOTO" />
DEBUG<input type="text" name="DEBUG"/>
<input type="submit" value="Add" />
</form>C#现已构造了一个API类用来向服务器提交数据,使用的是HttpWebRequest异步通信,代码如下:public class API
{
protected string baseUrl = @"http://xxxxx.xxxxx.com/api/";
protected string paramData = string.Empty;
public string resData = string.Empty;
public static string restitle;
public static string content;
public static string username;
public static string friend_name;
protected string url = string.Empty;
protected HttpStatusCode statusCode;
protected Exception errorAsync;
protected Encoding dataEncode = Encoding.UTF8; public void RequestURL()
{
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webReq);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
byte[] byteArray = dataEncode.GetBytes(paramData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
catch (Exception ee)
{
errorAsync = ee;
}
} private void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
resData = streamRead.ReadToEnd();
statusCode = response.StatusCode;
streamResponse.Close();
streamRead.Close();
JObject j = (JObject)JsonConvert.DeserializeObject(resData,typeof(JObject));
restitle = j.GetValue("title").ToString();
ProcessResponseData();
}
catch (Exception ee)
{
errorAsync = ee;
}
}
public virtual void ProcessResponseData()
{
//由子类对此函数不同的去实现
}
}若不提交图片,则在C#中构造参数的代码如下:this.paramData = @"EMAIL=" + username + @"&PASSWORD=" + password + @"&TEXT="+text+@"&PLACE_ID="+place_id+@"&DEBUG=yes";现在要想把文字和图片同时post到服务器上,该如何实现呢?
EMAIL<input type="text" name="EMAIL" />
PASSWORD<input type="text" name="PASSWORD"/>
TEXT<input type="text" name="TEXT"/>
</br>
PLACE_ID<input type="text" name="PLACE_ID"/>
PHOTO<input type="file" name="PHOTO" />
DEBUG<input type="text" name="DEBUG"/>
<input type="submit" value="Add" />
</form>C#现已构造了一个API类用来向服务器提交数据,使用的是HttpWebRequest异步通信,代码如下:public class API
{
protected string baseUrl = @"http://xxxxx.xxxxx.com/api/";
protected string paramData = string.Empty;
public string resData = string.Empty;
public static string restitle;
public static string content;
public static string username;
public static string friend_name;
protected string url = string.Empty;
protected HttpStatusCode statusCode;
protected Exception errorAsync;
protected Encoding dataEncode = Encoding.UTF8; public void RequestURL()
{
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webReq);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
byte[] byteArray = dataEncode.GetBytes(paramData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
catch (Exception ee)
{
errorAsync = ee;
}
} private void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
resData = streamRead.ReadToEnd();
statusCode = response.StatusCode;
streamResponse.Close();
streamRead.Close();
JObject j = (JObject)JsonConvert.DeserializeObject(resData,typeof(JObject));
restitle = j.GetValue("title").ToString();
ProcessResponseData();
}
catch (Exception ee)
{
errorAsync = ee;
}
}
public virtual void ProcessResponseData()
{
//由子类对此函数不同的去实现
}
}若不提交图片,则在C#中构造参数的代码如下:this.paramData = @"EMAIL=" + username + @"&PASSWORD=" + password + @"&TEXT="+text+@"&PLACE_ID="+place_id+@"&DEBUG=yes";现在要想把文字和图片同时post到服务器上,该如何实现呢?
解决方案 »
- TextBox 逐行显示的问题
- C#中对象资源释放的问题。
- 请教。关于文件的路径问题。
- 打印的问题:我想把pictureBox里的图片和图片上Label的内容打印在一起,该怎么做呢??
- 如何判断本机一个指定端口是否已经使用?
- MSSQL XML 查询是否有长度限制?
- 用System.Diagnostics.Process.Start("c:\\test.xls")打开Excel文档的怪问题
- VS2005默认的开发环境为C#,要如何才能转为VB.NET
- 异步回调发送变长消息
- Jmail 问题收发邮件报错检索 COM 类工厂中 CLSID 为 {F812B147-0E26-4222-8EE4-9F753CD2B39C} 的组件时失败
- delphi翻译成c#
- 对于组合查询,大家怎么封装啊?
---》multipart/form-data