我正在使用HttpWebRequest for Multipart Post以字节数组的形式将图像上传到服务器,我应该收到一个json字符串,其中包含响应中帖子的详细信息。但相比之下,我只得到一个响应代码“OK”作为响应,而不管帖子的成功与否。任何人都可以帮我解决这个问题。
我在帖子中使用以下代码。
Dictionary<string, object> postParam;
public void SubmitPost()
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(new Uri(url, UriKind.Absolute));
myRequest.Method = "POST";
myRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
public string url
{
get
{
return "URL";
}
}
string boundary = "----------" + DateTime.Now.Ticks.ToString();
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
writeMultipartObject(postStream, postParam);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
string res;
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
streamResponse.Close();
streamRead.Close();
res = response.StatusCode.ToString();
response.Close();
}
res的值总是“OK”
答案 0 :(得分:1)
您需要阅读streamRead
的回复。 StatusCode
只是表示数据已上传。