使用HttpWebRequest的帖子表单数据 [英] Post form data using HttpWebRequest

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

问题描述

我想某种形式的数据发布到这不是我自己的Web应用程序中指定的URL。它具有相同的域名,例如像domain.client.nl。 Web应用程序有一个网址web.domain.client.nl恩,我想发布到是idp.domain.client.nl的网址。
但我的code无助.....不会有人知道我在做什么错?

沃特

 的StringBuilder POSTDATA =新的StringBuilder();
postData.Append(HttpUtility.UrlEn code(的String.Format(用户名= {0}&放大器;的uname)));
postData.Append(HttpUtility.UrlEn code(的String.Format(密码= {0}&安培;PWORD)));
postData.Append(HttpUtility.UrlEn code(的String.Format(url_success = {0}&安培;urlSuccess)));
postData.Append(HttpUtility.UrlEn code(的String.Format(url_failed = {0},urlFailed)));ASCIIEncoding ASCII =新ASCIIEncoding();
字节[] = postBytes ascii.GetBytes(postData.ToString());//设置请求对象
HttpWebRequest的请求;
尝试
{
    请求=(HttpWebRequest的)HttpWebRequest.Create(WebSiteConstants.UrlIdp);
}
赶上(UriFormatException)
{
    请求= NULL;
}
如果(要求== NULL)
    抛出新ApplicationException的(无效的URL:+ WebSiteConstants.UrlIdp);request.Method =POST;
request.ContentType =应用/的X WWW的形式urlen codeD;
request.ContentLength = postBytes.Length;
request.UserAgent =Mozilla的/ 4.0(兼容; MSIE 6.0; Windows NT的5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727);//后的数据添加到请求
流postStream = request.GetRequestStream();
postStream.Write(postBytes,0,postBytes.Length);
postStream.Flush();
postStream.Close();


解决方案

这两个字段名称和值应该是URL连接codeD。
POST数据和查询字符串的格式都是一样的。

做的.NET的方法是这样的

 的NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(的String.Empty);
outgoingQueryString.Add(字段1,值1);
outgoingQueryString.Add(场2,值2);
字符串POSTDATA = outgoingQueryString.ToString();

这将编码字段和值名称的保健

I want to post some form data to a specified URL that isn't inside my own web application. It has the same domain, such like "domain.client.nl". The web application has a url "web.domain.client.nl" en the url where I want to post to is "idp.domain.client.nl". But my code does nothing..... does someone knows what I'm doing wrong?

Wouter

StringBuilder postData = new StringBuilder();
postData.Append(HttpUtility.UrlEncode(String.Format("username={0}&", uname)));
postData.Append(HttpUtility.UrlEncode(String.Format("password={0}&", pword)));
postData.Append(HttpUtility.UrlEncode(String.Format("url_success={0}&", urlSuccess)));
postData.Append(HttpUtility.UrlEncode(String.Format("url_failed={0}", urlFailed)));

ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData.ToString());

// set up request object
HttpWebRequest request;
try
{
    request = (HttpWebRequest)HttpWebRequest.Create(WebSiteConstants.UrlIdp);
}
catch (UriFormatException)
{
    request = null;
}
if (request == null)
    throw new ApplicationException("Invalid URL: " + WebSiteConstants.UrlIdp);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

// add post data to request
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();

解决方案

Both the field name and the value should be url encoded. format of the post data and query string are the same

The .net way of doing is something like this

NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
outgoingQueryString.Add("field1","value1");
outgoingQueryString.Add("field2", "value2");
string postdata = outgoingQueryString.ToString();

This will take care of encoding the fields and the value names

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

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