繁体   English   中英

C#HttpWebRequest /响应给出400错误的请求。 但不适用于Firefox HTTP Resource Test

[英]C# HttpWebRequest / Response gives 400 Bad Request. But not with Firefox HTTP Resource Test

我需要使用xml数据发出POST请求。

String xml = "";
byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
HttpClient.post(url, data, "text/xml")

然后我调用POST函数:

public static String post(String url, byte[] data, String contentType){
        String body = "";
        body = getResponse("POST", url, data, contentType, 80);
        return body;
    }

现在,我调用此函数进行请求/获取响应:

public static String getResponse(String method, String url, byte[] data, String contentType, int serverPort)
    {
        String result = null;
        HttpWebRequest request = sendRequest(method, url, data, contentType, serverPort);
        HttpWebResponse response = null;
        try
        {
            response = (HttpWebResponse)request.GetResponse();
            if (response != null){
                // Get the stream associated with the response
                Stream receiveStream = response.GetResponseStream ();
                // Pipes the stream to a higher level stream reader
                StreamReader readStream = new StreamReader (receiveStream, System.Text.Encoding.UTF8);
                result = readStream.ReadToEnd ();
            }
        }
        catch(WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError){
                throw new HttpClientException("HTTP response error. ", (int)(((HttpWebResponse)ex.Response).StatusCode), ((HttpWebResponse)ex.Response).StatusDescription);
            }
            else{
                throw new HttpClientException("HTTP response error with status: " + ex.Status.ToString());
            }
        }
}

public static HttpWebRequest sendRequest(String method, String url, byte[] data, String contentType, int serverPort){
        HttpWebRequest request = null;
        try
        {
            UriBuilder requestUri = new UriBuilder(url);
            requestUri.Port = serverPort;
            request = (HttpWebRequest)WebRequest.Create(requestUri.Uri);
            request.Method = method;
            //
            if ((method == "POST") && (data != null) && (data.Length > 0)){
                request.ContentLength = data.Length;
                request.ContentType = ((String.IsNullOrEmpty(contentType))?"application/x-www-form-urlencoded":contentType);
                Stream dataStream = request.GetRequestStream ();
                dataStream.Write (data, 0, data.Length);
                // Close the Stream object.
                dataStream.Close ();
            }
        }
        catch(WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError){
                throw new HttpClientException("HTTP request error. ", (int)(((HttpWebResponse)ex.Response).StatusCode), ((HttpWebResponse)ex.Response).StatusDescription);
            }
            else{
                throw new HttpClientException("HTTP request error with status: " + ex.Status.ToString());
            }
        }
}

它总是给我一个HttpCliendException

video_api.HttpClientException: HttpClient exception :HTTP response error.  with `code: 400` and `status: Bad Request`

但是,当我使用Firefox插件HTTP Resource Test尝试时,它运行良好并且使用相同的XML文档获得202 Accepted status

在调用发布请求之前,我先控制台了content-type和data.length,content-type是text / xml, data.length143

使用fiddler(http://www.fiddler2.com/fiddler2/)可以准确查看Firefox发送的标头。 然后查看您发送的标头中有什么不同。

我知道有些网站对请求标头很挑剔,仅根据这些值返回不同的结果。 比较FireFox中的HTTP请求的请求标头和您的请求,如果模仿FireFox资源测试中的标头,则它很可能起作用( Request.AddHeader("name", "value") )。 要注意的另一个区别可能是用户代理,它对于Web服务器仍然很挑剔。

在我的项目中,我在config.area中使用一些自定义设置

    <defaultProxy useDefaultCredentials="true">
...
    </defaultProxy>

它帮助我禁用了代理:

request.Proxy = null;

另一种解决方案是将SSL和NON-SSL端口弄乱。

当您将纯HTTP与https端口对话时,在Apache上回答400错误请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM