How to use HttpWebRequest to send POST request to another web server?
2005-03-26 23:10
671 查看
How to use HttpWebRequest to send POST request to another web server?
要提交象以下格式的表单:<form name="_xclick" target="paypal"
action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="me@mybiz.com">
<input type="hidden" name="item_name" value="HTML book">
<input type="hidden" name="amount" value="24.99">
<input type="image" src="http://www.paypal.com/images/sc-but-01.gif"
border="0" name="submit" alt="Make payments with PayPal!">
<input type="hidden" name="add" value="1">
</form>
POST方法写法如下:
private void OnPostInfoClick(object sender, System.EventArgs e)
{
string strId = UserId_TextBox.Text;
string strName = Name_TextBox.Text;
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
//读取response
res = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding(utf-8);
StreamReader sr = new StreamReader( ReceiveStream, encode );
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
while (count > 0)
{
String str = new String(read, 0, count);
strResult = str;
count = sr.Read(read, 0, 256);
}
}
相关文章推荐
- How to use HttpWebRequest to send POST request to another web server?
- How to use HttpWebRequest to post data to another page which is on another server
- How to use HttpURLConnection POST data to web server?
- How to use HttpURLConnection POST data to web server?
- How to use HttpWebRequest and HttpWebResponse in .NET
- How to send HTTP request GET/POST in Java
- How to use the HttpWebRequest object
- How to use HttpWebRequest and RegularExpressions to crash website
- How to use HttpSendRequestEx with password-protected URLs
- How to use GET and POST methods in HTTP from a MIDlet
- Web service request SetParameters to Report Server http://host/reportserver failed. Error: 请求因 HTTP 状态 401 失败: Unauthorized
- rc : how to use HttpSendRequestEx
- How to resolve the 403 error when send POST request from Postman
- How to use FtpWebRequest in asp.net
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- Web service request SetParameters to Report Server http://host/reportserver failed. Error: 请求因 HTTP 状态 401 失败: Unauthorized
- How to send HTTP request in java?
- How to Send an HTTP Header With Every Request With Spring RestTemplate
- How To Use Proxy Server To Access Internet at Shell Prompt With http_proxy Variable. [reprint]
- 【ABAP】Creat a client-server demo to process a http request using SAP Web AS