C# System.Net.WebException:获取响应流时出错(ReadDone2)
当我尝试将代码移植到mono时,出现了以下错误。它在windows中运行良好,甚至可以在Linux中编译 当我使用HttpWebRequest(一个按钮调用一个向流中执行写操作的方法,等等)时,它抛出这个错误C# System.Net.WebException:获取响应流时出错(ReadDone2),c#,mono,httpwebrequest,webexception,C#,Mono,Httpwebrequest,Webexception,当我尝试将代码移植到mono时,出现了以下错误。它在windows中运行良好,甚至可以在Linux中编译 当我使用HttpWebRequest(一个按钮调用一个向流中执行写操作的方法,等等)时,它抛出这个错误 System.Net.WebException: Error getting response stream (ReadDone2): ReceiveFailure ---> System.Exception: at System.Net.WebConnection.H
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", "0", PostDataParamType.Field));
byte[] buff = encoding.GetBytes(pData.GetPostData());
oRequest.ContentLength = buff.Length;
oStream = oRequest.GetRequestStream();
oStream.Write(buff, 0, 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, string value, PostDataParamType type)
{
Name = name;
Value = value;
FileName = filename;
Type = type;
}
public string Name;
public string FileName;
public string Value;
public PostDataParamType Type;
}
公共类PostData
{
//如果需要,请更改此选项,无需更改
公共静态字符串边界=“AaB03x”;
私有列表m_参数;
公共列表参数
{
获取{return m_Params;}
设置{m_Params=value;}
}
公共PostData()
{
m_Params=新列表();
}
///
///返回为多部分/表单数据格式化的参数数组
///
///
公共字符串GetPostData()
{
StringBuilder sb=新的StringBuilder();
foreach(m_参数中的PostDataParam p)
{
sb.附加线(“-”+边界);
if(p.Type==PostDataParamType.File)
{
sb.AppendLine(string.Format(“内容配置:文件;名称=\”{0}\“文件名=\”{1}\”,p.name,p.filename));
sb.AppendLine(“内容类型:应用程序/八位字节流”);
(某人);
sb.附加线(p.值);
}
其他的
{
sb.AppendLine(string.Format(“内容处置:表单数据;名称=\”{0}\”,p.name));
(某人);
sb.附加线(p.值);
}
}
sb.附录行(“-”+边界+“-”);
使某人返回字符串();
}
}
公共枚举PostDataParamType
{
领域
文件
}
公共类PostDataParam
{
公共PostDataParam(字符串名称、字符串值、PostDataParamType类型)
{
名称=名称;
价值=价值;
类型=类型;
}
公共PostDataParam(字符串名称、字符串文件名、字符串值、PostDataParamType类型)
{
名称=名称;
价值=价值;
FileName=文件名;
类型=类型;
}
公共字符串名称;
公共字符串文件名;
公共字符串值;
公共PostDataParamType类型;
}
此代码仍然不完整:PostData从何而来?我在MSDN上找不到它。这个代码仍然不完整:PostData从哪里来?我在MSDN上找不到它。