HttpWebRequest.GetResponse()抛出“错误请求”400错误

时间:2012-07-26 01:04:12

标签: c# .net-4.0 httpwebrequest httpwebresponse bad-request

以下是带有(更改的实际URL和凭据)的示例代码

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback((object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return true; }); 

var requestUri = new Uri("https://example.com/path1/path2?var=somevar");

byte[] data = Encoding.UTF8.GetBytes("DEV72*12/27/06*TAYLOR*RICH*JOE*10/28/56*U0999495102*466666666*01*TAYLOR*TAMMY*M*10/26/52***CIGNA HMO*62308*UPIN*MEDICAL CENTER**MESSAGE*M*F*30");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.PreAuthenticate = true;
request.UseDefaultCredentials = false;
request.Credentials = new NetworkCredential("UUUSSSEEERR", "PASSWORD");
request.ContentLength = data.Length;
request.ContentType = "text/plain";
request.Method = "POST";

using (Stream stream = request.GetRequestStream())
{
   stream.Write(data, 0, data.Length);
   stream.Flush();
}

StreamReader streamReader = null;

string result;
try
{
   streamReader = new StreamReader(request.GetResponse().GetResponseStream());
   result = streamReader.ReadToEnd();
}
finally
{
   streamReader.Close();
}

txtOutput.Text = result;

它总是在第一次运行时抛出错误的Request异常,但是当我将调试器中的光标移回第一行时它工作正常并在txtOutput.text中给出正确的响应,并在分离的应用程序中提供相同的行为,第一次抛出异常但是第二次时间它工作正常,有人可以给我一些工作或让我知道我在这里做了什么错。 使用.net 4.0。

更新

例如以下代码对我有用,但这不是我正在寻找的工作我只是通过捕获WebException添加了一个while循环,如果它的statusCode是400我重新启动代码并且它可以工作

var requestUri = new Uri("https://example.com/path1/path2?var=somevar");

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback((object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return true; }); 

byte[] data = Encoding.UTF8.GetBytes("DEV72*12/27/06*TAYLOR*RICH*JOE*10/28/56*U0999495102*466666666*01*TAYLOR*TAMMY*M*10/26/52***CIGNA HMO*62308*UPIN*MEDICAL CENTER**MESSAGE*M*F*30");
HttpWebRequest request = null;  
int counter = 0;
do
{
    request = (HttpWebRequest)WebRequest.Create(requestUri);

    request.PreAuthenticate = true;
    request.UseDefaultCredentials = false;
    request.Credentials = new NetworkCredential("USERNAME", "password");
    request.ContentLength = data.Length;
    request.ContentType = "text/plain";
    request.Method = "POST";

    using (Stream stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
        stream.Flush();
    }

    StreamReader streamReader = null;

    string result = string.Empty;
    try
    {
        streamReader = new StreamReader(request.GetResponse().GetResponseStream());
        result = streamReader.ReadToEnd();
    }
    catch (WebException ex)
    {
        HttpWebResponse response = (HttpWebResponse)ex.Response;
        if (response.StatusCode == HttpStatusCode.BadRequest)
        {
            counter++;
        }
        else
        {
            txtOutput.Text = ex.ToString();
        }
    }
    finally
    {
        if(streamReader != null) streamReader.Close();
    }

    txtOutput.Text = result;

} while (counter == 1);

1 个答案:

答案 0 :(得分:0)

stream.Close();在stream.Flush()之后;可能可能有帮助