System.Net.WebException:获取响应流时出错(ReadDone2)

时间:2011-05-25 17:04:29

标签: c# mono httpwebrequest webexception

当我尝试将代码移植到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", "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;
}

0 个答案:

没有答案