标签: webresponse

c#Webrequest Post和GetResponse

我正在编写一个程序,它将向网站提交XML.编写的代码工作正常,有时它因某些原因停止工作,抛出System.Net.ProtocolViolationException.我可以关闭程序并重新运行 - 它再次开始工作就好了.

这是我正在使用的代码:

private string Summit(string xml)
{
    string result = string.Empty;
    StringBuilder sb = new StringBuilder();
    try {
        WebRequest request = WebRequest.Create(this.targetUrl);
        request.Timeout = 800 * 1000;

        RequestState requestState = new RequestState(xml);
        requestState.Request = request;
        request.ContentType = "text/xml";

        // Set the 'Method' property  to 'POST' to post data to a Uri.
        requestState.Request.Method = "POST";
        requestState.Request.ContentType = "text/xml";

        // Start the Asynchronous 'BeginGetRequestStream' method call.    
        IAsyncResult r = (IAsyncResult)request.BeginGetRequestStream(new AsyncCallback(ReadCallBack), requestState);

        // Pause the current thread until …
Run Code Online (Sandbox Code Playgroud)

c# webrequest webresponse iasyncresult

3
推荐指数
1
解决办法
2万
查看次数

WebResponse在实现IDisposable时如何不公开显示"Dispose"?

通过最近涉及WebResponse的一些代码调试后,我发现我遇到的问题是我在发出另一个之前没有正确处理WebResponse.因为WebResponse需要被转换为IDisposable以便实际调用dispose(或者你可以使用"using"来实现相同的目标),所以我误入歧途.

所以我的问题是:

1)Microsoft用什么来实现这一目标?

IDisposable是一个接口,因此是公共的,但WebResponse以某种方式改变了根据MSDN doumentation保护的访问修饰符.我认为这是不可能的.

2)以这种方式隐藏处置有什么好处?

为什么不让webResponse.Dispose()有效?

.net c# webresponse httpwebresponse

3
推荐指数
1
解决办法
227
查看次数

在HTTP身份验证中读取Realm属性

如何读取请求HTTP基本身份验证的服务器在WWW-Authenticate标头中发送的Realm属性?

c# webresponse httpwebrequest basic-authentication

2
推荐指数
1
解决办法
2936
查看次数

关闭WebResponse,还是保持打开状态?

所以我正在制作一个几乎可以制作批量HttpWebRequests的程序.在这个程序中,速度是主要的.如果我能找到将HttpWebRequests增加甚至毫秒的方法,那就非常好了.

好的,我的问题是这样的:我有一个方法对一个站点发出HttpWebRequest(GET请求),另一个方法是对一个主机发出一个POST HttpWebRequest(略有不同的URL,但是同一个主机),这个方法叫做AFTER每隔一段时间使用第一种方法.

在我的第一个方法(GET请求,假设方法A)中,我在读取响应主体后关闭了WebResponse.让这个WebResponse保持打开状态,然后调用POST方法(让我们说方法B),或者我应该如何实现它,从方法A关闭WebResponse是不是更快?

示例代码:

public string MethodA()
{
   // Make a HttpWebRequest to a URL like: xxxx.yyyy.com
   WebResponse response = request.GetResponse();
   string x = ReadResponseBody(response);
   response.Close();
   if(x.Contains("something"))
      MethodB();
}

public void MethodB()
{
   // Make a POST HttpWebRequest to a URL like: xxxx.zzzz.com (same host).
   WebResponse response = request.GetResponse();
   response.Close();
}
Run Code Online (Sandbox Code Playgroud)

那么,我应该保留我的代码,关闭MethodA()中的第一个WebResponse,然后调用MethodB(),还是其他什么?

此外,任何人都可以提供一些关于如何提高速度的提示,因为这是我的程序中最重要的事情,我需要尽可能快.

c# sockets webrequest webresponse httpwebrequest

2
推荐指数
1
解决办法
1227
查看次数

HttpClient 响应返回乱码数据

我正在尝试使用 HttpClient 从此 URL 获取正确的 JSON 响应。当我在 Chrome 中查看 URL 时,数据是正确格式的 JSON。当我使用 HttpClient 时,我收到一堆看起来像字节或类似内容的垃圾数据。我不知道如何将其解码为字符串。请指教。

string url = "https://api.nasdaq.com/api/calendar/earnings?date=2010-07-30";

string calendar = await DownloadFile(new string[] { url });

private static readonly HttpClient httpClient = new HttpClient();

        public static async Task<string> DownloadFile(string[] args)
        {
            string url = args[0];

            httpClient.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate, br");
            httpClient.DefaultRequestHeaders.Connection.ParseAdd("keep-alive");

            string text = await httpClient.GetStringAsync(url);
            
            return text;

        }
Run Code Online (Sandbox Code Playgroud)

c# encoding webresponse httpclient

2
推荐指数
1
解决办法
586
查看次数