问题描述
||
尝试将代码移植到mono时出现以下错误。它在Windows中运行良好,甚至可以在Linux中编译。
当我使用HttpWebRequest时(一个按钮调用对流执行写操作的方法,等等),它将引发此错误。
System.Net.WebException: Error getting response stream (ReadDone2): ReceiveFailure ---> System.Exception:
at System.Net.WebConnection.HandleError(WebExceptionStatus st,System.Exception e,System.String where)
at System.Net.WebConnection.ReadDone(IAsyncResult result)
at System.Net.Sockets.Socket+SocketAsyncResult.Complete()
at System.Net.Sockets.Socket+Worker.Receive()
该代码是
HttpWebRequest oRequest;
PostData pData = new PostData();
Encoding encoding = Encoding.UTF8;
Stream oStream = null;
byte[] buffer;
if (!isLoggedIn)
{
oRequest = (HttpWebRequest)HttpWebRequest.Create(\"http://www.ksl.com/public/member/signin\");
oRequest.CookieContainer = cookieJar;
oRequest.ContentType = \"multipart/form-data; boundary=\" + PostData.boundary;
oRequest.Method = \"POST\";
pData.Params.Add(new PostDataParam(\"MAX_FILE_SIZE\",\"50000000\",PostDataParamType.Field));
pData.Params.Add(new PostDataParam(\"dado_form_3\",\"1\",PostDataParamType.Field));
pData.Params.Add(new PostDataParam(\"member[email]\",Properties.Settings.Default.txtusername,PostDataParamType.Field));
pData.Params.Add(new PostDataParam(\"member[password]\",Properties.Settings.Default.txtpassword,PostDataParamType.Field));
pData.Params.Add(new PostDataParam(\"x\",\"0\",PostDataParamType.Field));
pData.Params.Add(new PostDataParam(\"y\",PostDataParamType.Field));
byte[] buff = encoding.GetBytes(pData.GetPostData());
oRequest.ContentLength = buff.Length;
oStream = oRequest.GetRequestStream();
oStream.Write(buff,buff.Length);
oRequest.GetResponse().GetResponseStream();
isLoggedIn = true;
}
类PostData的定义如下:
public class PostData
{
// Change this if you need to,not necessary
public static string boundary = \"AaB03x\";
private List<PostDataParam> m_Params;
public List<PostDataParam> Params
{
get { return m_Params; }
set { m_Params = value; }
}
public PostData()
{
m_Params = new List<PostDataParam>();
}
/// <summary>
/// Returns the parameters array formatted for multi-part/form data
/// </summary>
/// <returns></returns>
public string GetPostData()
{
StringBuilder sb = new StringBuilder();
foreach (PostDataParam p in m_Params)
{
sb.AppendLine(\"--\" + boundary);
if (p.Type == PostDataParamType.File)
{
sb.AppendLine(string.Format(\"Content-Disposition: file; name=\\\"{0}\\\"; filename=\\\"{1}\\\"\",p.Name,p.FileName));
sb.AppendLine(\"Content-Type: application/octet-stream\");
sb.AppendLine();
sb.AppendLine(p.Value);
}
else
{
sb.AppendLine(string.Format(\"Content-Disposition: form-data; name=\\\"{0}\\\"\",p.Name));
sb.AppendLine();
sb.AppendLine(p.Value);
}
}
sb.AppendLine(\"--\" + boundary + \"--\");
return sb.ToString();
}
}
public enum PostDataParamType
{
Field,File
}
public class PostDataParam
{
public PostDataParam(string name,string value,PostDataParamType type)
{
Name = name;
Value = value;
Type = type;
}
public PostDataParam(string name,string filename,PostDataParamType type)
{
Name = name;
Value = value;
FileName = filename;
Type = type;
}
public string Name;
public string FileName;
public string Value;
public PostDataParamType Type;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)