如何添加参数到WebRequest的? [英] How to add parameters into a WebRequest?

查看:291
本文介绍了如何添加参数到WebRequest的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用从web服务的方法,所以我写了这个code:

I need to call a method from a webservice, so I've written this code:

 private string urlPath = "http://xxx.xxx.xxx/manager/";
                string request = urlPath + "index.php/org/get_org_form";
                WebRequest webRequest = WebRequest.Create(request);
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.
                webRequest.ContentLength = 0;
                WebResponse webResponse = webRequest.GetResponse();

不过,这种方法需要一些参数,如下:

But this method requires some parameters, as following:

发布数据:

_username:API用户',// API认证的用户名

_username:'API USER', // api authentication username

_password:API密码',// API认证 密码

_password:'API PASSWORD', // api authentication password

我如何添加这些参数到这个WebRequest的?

How can I add these parameters into this Webrequest?

在此先感谢。

推荐答案

如果这是URL字符串的参数,那么你需要通过添加这些?和'&安培;'字符,例如<一个href="http://mysite.com/index.aspx?username=Api_user&password=Api_password">http://mysite.com/index.aspx?username=Api_user&password=Api_password.

If these are the parameters of url-string then you need to add them through '?' and '&' chars, for example http://mysite.com/index.aspx?username=Api_user&password=Api_password.

如果这是POST请求的参数,那么你需要创建POST数据,并将其写入请求流。下面是示例方式:

If these are the parameters of POST request, then you need to create POST data and write it to request stream. Here is sample method:

private static string doRequestWithBytesPostData(string requestUri, string method, byte[] postData,
                                        CookieContainer cookieContainer,
                                        string userAgent, string acceptHeaderString,
                                        string referer,
                                        string contentType, out string responseUri)
        {
            var result = "";
            if (!string.IsNullOrEmpty(requestUri))
            {
                var request = WebRequest.Create(requestUri) as HttpWebRequest;
                if (request != null)
                {
                    request.KeepAlive = true;
                    var cachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
                    request.CachePolicy = cachePolicy;
                    request.Expect = null;
                    if (!string.IsNullOrEmpty(method))
                        request.Method = method;
                    if (!string.IsNullOrEmpty(acceptHeaderString))
                        request.Accept = acceptHeaderString;
                    if (!string.IsNullOrEmpty(referer))
                        request.Referer = referer;
                    if (!string.IsNullOrEmpty(contentType))
                        request.ContentType = contentType;
                    if (!string.IsNullOrEmpty(userAgent))
                        request.UserAgent = userAgent;
                    if (cookieContainer != null)
                        request.CookieContainer = cookieContainer;

                    request.Timeout = Constants.RequestTimeOut;

                    if (request.Method == "POST")
                    {
                        if (postData != null)
                        {
                            request.ContentLength = postData.Length;
                            using (var dataStream = request.GetRequestStream())
                            {
                                dataStream.Write(postData, 0, postData.Length);
                            }
                        }
                    }

                    using (var httpWebResponse = request.GetResponse() as HttpWebResponse)
                    {
                        if (httpWebResponse != null)
                        {
                            responseUri = httpWebResponse.ResponseUri.AbsoluteUri;
                            cookieContainer.Add(httpWebResponse.Cookies);
                            using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
                            {
                                result = streamReader.ReadToEnd();
                            }
                            return result;
                        }
                    }
                }
            }
            responseUri = null;
            return null;
        }

这篇关于如何添加参数到WebRequest的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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