WebClient的HTTP帖子 [英] Http post wth WebClient

查看:97
本文介绍了WebClient的HTTP帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:
我是.net编程的新手,需要一些帮助.我的意图是将数据发布到Web服务器并获得所需的服务.我认为该程序的功能是:
在使用用户名/密码进行初始登录会话之后,以下会话仅接受所需服务的键值对-这是服务器应提供的服务的方式.

我找到了一个Cookie唤醒的WebClient类的实现代码,它可能是一个解决方案,如下所示.但这是行不通的.问题是:第一个会话中的cookie没有被很好地捕获,因此后续会话缺少所需的信息才能正常工作.

关于如何实现该功能,还有更详细的示例吗?

提前谢谢!

雅各布

Hello everyone:
I am new in .net programming and need some help. My intention is to post data to a web server and get desired service. The functionality of the program in my mind is that:
After an initial log-in session with username/password, the following sessions only accepts key-value pairs for a required service -- this is the way the server supposed to provide the service.

I found an implementation code for a Cookie-awared WebClient class that may be a solution, attached as following. But it does not work. The problem is: the cookies from the first session was not well captured, so subsequent sessions lack the desired information to work properly.

Is there a more detailed example on how to realize the function?

Thank you in advance!

Jacob

public class WebClient_CookieAware : WebClient
 {
     public CookieContainer theCookieContainer { get; set; }
     public Uri Uri { get; set; }
     public WebClient_Cookie(): this(new CookieContainer())
          {    }

     public WebClient_Cookie(CookieContainer cookies)
        {
         this.theCookieContainer = cookies;
         }

     protected override WebRequest GetWebRequest(Uri address)
     {
         WebRequest request = base.GetWebRequest(address);
         if (request is HttpWebRequest)
             { (request as HttpWebRequest).CookieContainer = theCookieContainer;}
         HttpWebRequest httpRequest = (HttpWebRequest)request;
         // httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
         return httpRequest;
     }

     protected override WebResponse GetWebResponse(WebRequest request)
     {
         WebResponse response = base.GetWebResponse(request);
         String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
         if (setCookieHeader == null)
         {
             Cookie cookie = new Cookie();
             this.theCookieContainer.Add(cookie);
         }
         return response;
     }
 }

推荐答案

看看下面的代码:
Take a look at this code:
WebRequest webRequest;
WebResponse webResponse;
string string_Field = webResponse.Headers[HttpResponseHeader.SetCookie];
webRequest.Headers.Add(HttpRequestHeader.Cookie, string_Field);


这篇关于WebClient的HTTP帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆