6

お願い助けて。ポストクエリを送信した後、「応答ストリームの取得中にエラーが発生しました (ReadDone2): 受信に失敗しました」という webexception があります。このエラーを取り除くのに役立ちます。ありがとう。

コード片

try
{
string queryContent = string.Format("login={0}&password={1}&mobileDeviceType={2}/",
login, sessionPassword, deviceType);
request = ConnectionHelper.GetHttpWebRequest(loginPageAddress, queryContent);

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())//after this line //occurs exception - "Error getting response stream (ReadDone2): Receive Failure"
{

ConnectionHelper.ParseSessionsIdFromCookie(response);

string location = response.Headers["Location"];
if (!string.IsNullOrEmpty(location))
{
string responseUri = Utils.GetUriWithoutQuery(response.ResponseUri.ToString());
string locationUri = Utils.CombineUri(responseUri, location);
result = this.DownloadXml(locationUri);
}
response.Close();
}
}
catch (Exception e)
{
errorCout++;
errorText = e.Message;
}

//メソッド GetHttpWebRequest

    public static HttpWebRequest GetHttpWebRequest(string uri, string queryContent)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);            
        request.Proxy = new WebProxy(uri);
        request.UserAgent = Consts.userAgent;
        request.AutomaticDecompression = DecompressionMethods.GZip;
        request.AllowWriteStreamBuffering = true;
        request.AllowAutoRedirect = false;

        string sessionsId = GetSessionsIdForCookie(uri);
        if (!string.IsNullOrEmpty(sessionsId))
            request.Headers.Add(Consts.headerCookieName, sessionsId);

        if (queryContent != string.Empty)
        {
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            byte[] SomeBytes = Encoding.UTF8.GetBytes(queryContent);
            request.ContentLength = SomeBytes.Length;
            using (Stream newStream = request.GetRequestStream())
            {
                newStream.Write(SomeBytes, 0, SomeBytes.Length);
            }
        }
        else
        {
            request.Method = "GET";
        }

        return request;
    }
4

3 に答える 3

0
using (Stream newStream = request.GetRequestStream())
{
    newStream.Write(SomeBytes, 0, SomeBytes.Length);

    //try to add
    newStream.Close();
}
于 2011-09-12T14:59:19.490 に答える