急!!!!100分求一个http post 发送和接收数据的完整示例

wangdehao 2005-08-25 02:03:03
见标题
...全文
325 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fffgtao 2006-04-03
  • 打赏
  • 举报
回复
jedliu 2005-09-09
  • 打赏
  • 举报
回复
syeerzy(快乐永远*先天下之乐而乐*后天下之忧而忧*) 说的对,楼主这样学习不太好,可能你程序会编,但基础不牢,可能出现的问题就是:别人问你问题,你知道,但说出来别人不懂。
salmon230 2005-09-09
  • 打赏
  • 举报
回复
我是来学习的
boyyao 2005-09-09
  • 打赏
  • 举报
回复
if (this.bHandleCookies)
{
if (Response.Cookies.Count > 0)
{
if (this.oCookies == null)
{
this.oCookies = Response.Cookies;
}
else
{
// ** If we already have cookies update the list
foreach (Cookie oRespCookie in Response.Cookies)
{
bool bMatch = false;
foreach (Cookie oReqCookie in this.oCookies)
{
if (oReqCookie.Name == oRespCookie.Name)
{
oReqCookie.Value = oRespCookie.Value;
bMatch = true;
break; //
}
} // for each ReqCookies
if (!bMatch)
this.oCookies.Add(oRespCookie);
} // for each Response.Cookies
} // this.Cookies == null
} // if Response.Cookie.Count > 0
} // if this.bHandleCookies = 0


// *** Save the response object for external access
Encoding enc;
try
{
if (Response.ContentEncoding.Length > 0)
enc = Encoding.GetEncoding(Response.ContentEncoding);
else
enc = Encoding.GetEncoding(cEncoding);
}
catch
{
// *** Invalid encoding passed
enc = Encoding.GetEncoding(cEncoding);
}

// *** drag to a stream
StreamReader strResponse = new StreamReader(Response.GetResponseStream(), enc);
string str = strResponse.ReadToEnd();
Response.Close();
strResponse.Close();
//自动跟踪引用页
if (this.bHandleReferer)
{
this.cReferer = Url;
}
//自动处理HTTP/1.0 302 Moved Temporarily中的Location后的页面。(自动完成跳转)
if (this.bLocation)
{

//这里需要自动获得跳转页面的地址。并且再次使用这个方法访问页面
}
return str;
}
catch (Exception e)
{
if (this.bThrowExceptions)
throw e;
this.cErrorMsg = e.Message;
this.bError = true;
return null;
}
}








private string UrlEncode(string url)
{
byte[] bs=Encoding.GetEncoding("gb2312").GetBytes(url);
StringBuilder sb=new StringBuilder();
for(int i=0;i<bs.Length;i++)
{
if(bs[i]<128)
sb.Append((char)bs[i]);
else
{
sb.Append("%"+bs[i++].ToString("x").PadLeft(2,'0'));
sb.Append("%"+bs[i].ToString("x").PadLeft(2,'0'));
}
}
return sb.ToString();
}




















}
}

boyyao 2005-09-09
  • 打赏
  • 举报
回复
public void AddPostKey(string Key, string Value)
{
cPostData += Key + "=" + System.Web.HttpUtility.UrlEncode(Value, System.Text.Encoding.GetEncoding("GB2312")) + "&";
}

/// <summary>
/// 增加提交的连续值(完整或者部分完整值)
/// </summary>
/// <param name="FullPostBuffer"></param>
public void AddPostKey(string FullPostBuffer)
{
cPostData += FullPostBuffer;
}







public string GetUrl(string Url)
{
Url = UrlEncode(Url);
Debug.WriteLine(Url);
try
{
this.bError = false;
this.cErrorMsg = "";

//通用的属性
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(Url);
Request.UserAgent = this.cUserAgent;
Request.Timeout = this.nConnectTimeout * 1000;
Request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*";
Request.Referer = this.cReferer;
//Request.Connection = "keep-alive";

// 需要安全验证的访问
if (this.cUsername.Length > 0)
{
if (this.cUsername == "AUTOLOGIN")
Request.Credentials = CredentialCache.DefaultCredentials;
else
Request.Credentials = new NetworkCredential(this.cUsername, this.cPassword);
}

// 需要使用Proxy和其配置
if (this.cProxyAddress.Length > 0)
{
if (this.cProxyAddress == "DEFAULTPROXY")
{
Request.Proxy = new WebProxy();
Request.Proxy = WebProxy.GetDefaultProxy();
}
else
{
WebProxy loProxy = new WebProxy(this.cProxyAddress, true);
if (this.cProxyBypass.Length > 0)
{
loProxy.BypassList = this.cProxyBypass.Split(';');
}

if (this.cProxyUsername.Length > 0)
loProxy.Credentials = new NetworkCredential(this.cProxyUsername, this.cProxyPassword);

Request.Proxy = loProxy;
}
}

// 需要操作Cookies和自动重用Cookies
if (this.bHandleCookies)
{
Request.CookieContainer = new CookieContainer();
if (this.oCookies != null && this.oCookies.Count > 0)
{
Request.CookieContainer.Add(this.oCookies);
}
}

Request.Method = cMethod;//设置提交模式

if (this.cMethod == "POST")
{
Request.ContentType = "application/x-www-form-urlencoded";
if (this.cPostData.EndsWith("&"))
this.cPostData = this.cPostData.Substring(0, this.cPostData.Length - 1);

//MessageBox.Show(this.cPostData);

byte[] lbPostBuffer = System.Text.Encoding.GetEncoding(cEncoding).GetBytes(cPostData);
Request.ContentLength = lbPostBuffer.Length;
Stream loPostData = Request.GetRequestStream();

loPostData.Write(lbPostBuffer, 0, lbPostBuffer.Length);
loPostData.Close();

// *** clear the POST buffer
this.cPostData = "";
}

// *** Retrieve the response headers
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

// ** Save cookies the server sends
boyyao 2005-09-09
  • 打赏
  • 举报
回复
/*
MyHttp访问类,参考了一些他人的方法.还未完善.希望有人能完善后在给我一份.
作者:Boyyao@msn.com
QQ:341591
ICQ:24158010

使用方法:
MyHttp loHttp = new MyHttp();
string lcHtml = "";

loHttp.HandleCookies = true;//操作Cookies
loHttp.Method = "GET";
lcHtml = loHttp.GetUrl("http://signin.ebay.com.cn/ws2/eBayISAPI.dll?SignIn&ssPageName=h:h:sout:CN");

loHttp.AddPostKey("Key", "Value");
loHttp.Referer = "http://signin.ebay.com.cn/ws2/eBayISAPI.dll?SignIn&ssPageName=h:h:sout:CN";

loHttp.Method = "POST";
lcHtml = loHttp.GetUrl("http://signin.ebay.com.cn/ws2/eBayISAPI.dll");

MessageBox.Show(loHttp.ErrorMsg);
MessageBox.Show(lcHtml);

*/

using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Windows.Forms;//only For Use MessageBox
using System.Net;
using System.IO;
using System.Diagnostics;

namespace MyTools.NetWork.Http
{
public class MyHttp
{
/// <summary>
/// User name used for Authentication.
/// To use the currently logged in user when accessing an NTLM resource you can use "AUTOLOGIN".
/// </summary>
public string Username
{
get { return this.cUsername; }
set { cUsername = value; }
}

/// <summary>
/// Password for Authentication.
/// </summary>
public string Password
{
get { return this.cPassword; }
set { this.cPassword = value; }
}

/// <summary>
/// Address of the Proxy Server to be used.
/// Use optional DEFAULTPROXY value to specify that you want to IE's Proxy Settings
/// </summary>
public string ProxyAddress
{
get { return this.cProxyAddress; }
set { this.cProxyAddress = value; }
}

/// <summary>
/// Semicolon separated Address list of the servers the proxy is not used for.
/// </summary>
public string ProxyBypass
{
get { return this.cProxyBypass; }
set { this.cProxyBypass = value; }
}

/// <summary>
/// Username for a password validating Proxy. Only used if the proxy info is set.
/// </summary>
public string ProxyUsername
{
get { return this.cProxyUsername; }
set { this.cProxyUsername = value; }
}
/// <summary>
/// Password for a password validating Proxy. Only used if the proxy info is set.
/// </summary>
public string ProxyPassword
{
get { return this.cProxyPassword; }
set { this.cProxyPassword = value; }
}

/// <summary>
/// Timeout for the Web request in seconds. Times out on connection, read and send operations.
/// Default is 30 seconds.
/// </summary>
public int Timeout
{
get { return this.nConnectTimeout; }
set { this.nConnectTimeout = value; }
}



public bool HandleReferer
{
get { return this.bHandleReferer; }
set { this.bHandleReferer = value; }
}

/// <summary>
/// 引用页
/// </summary>
public string Referer
{
get { return this.cReferer; }
set { this.cReferer = value; }
}

/// <summary>
/// 提交模式,默认是POST,用GET模式的时候不能使用PostData
/// </summary>
/// <value></value>
public string Method
{
get { return this.cMethod; }
set { this.cMethod = value; }
}
/// <summary>
/// Error Message if the Error Flag is set or an error value is returned from a method.
/// </summary>
public string ErrorMsg
{
get { return this.cErrorMsg; }
set { this.cErrorMsg = value; }
}

/// <summary>
/// Error flag if an error occurred.
/// </summary>
public bool Error
{
get { return this.bError; }
set { this.bError = value; }
}

/// <summary>
/// Determines whether errors cause exceptions to be thrown. By default errors
/// are handled in the class and the Error property is set for error conditions.
/// (not implemented at this time).
/// </summary>
public bool ThrowExceptions
{
get { return bThrowExceptions; }
set { this.bThrowExceptions = value; }
}

/// <summary>
/// If set to a non-zero value will automatically track cookies. The number assigned is the cookie count.
/// </summary>
public bool HandleCookies
{
get { return this.bHandleCookies; }
set { this.bHandleCookies = value; }
}
//Cookies集合
public CookieCollection Cookies
{
get { return this.oCookies; }
set { this.Cookies = value; }
}

//默认的编码
public string MyEncoding
{
get { return this.cEncoding; }
set { this.cEncoding = value; }
}

//自动跳转到新的页面
public bool Location
{
get { return this.bLocation; }
set { this.bLocation = value; }
}
// *** member properties
string cPostData = ""; //提交的数据
int nConnectTimeout = 180; //超时
string cUserAgent = "Mozilla/4.0"; //用户代理
bool bHandleReferer = true; //自动操作引用页
string cReferer = ""; //引用页
string cMethod = "POST"; //提交模式POST ro GET
string cUsername = "";
string cPassword = "";
string cProxyAddress = "";
string cProxyBypass = "";
string cProxyUsername = "";
string cProxyPassword = "";
bool bThrowExceptions = true; //是否抛出异常
bool bHandleCookies = true; //自动操作Cookies
CookieCollection oCookies;
string cErrorMsg = ""; //错误返回
bool bError = false;
string cEncoding = "GB2312";//UTF-8 GB2312
bool bLocation = false;

public MyHttp() { }

/// <summary>
/// 增加提交的值
/// </summary>
/// <param name="Key"></param>
/// <param name="Value"></param>
syeerzy 2005-09-09
  • 打赏
  • 举报
回复
回复: wangdehao(找找找)
小弟不懂.net,所以需要一个完整的实例,就是一个拷贝过来直接就能运行的





不懂.net,如果是想学,需要的是一本好书,不是一个拷贝过来直接就能运行的代码。
如果不打算学,仅仅是来“买程序”的,100分的价格太瞧不起这里些兄弟的劳动了,因为你买的是“企业版”,不是“开发版”,虽然功能一样,建议你看看企业版的SqlServer和开发版的价格相差多少(功能完全一样)
chinawn 2005-09-09
  • 打赏
  • 举报
回复
http://dragon.cnblogs.com/archive/2005/06/15/174946.html
wangdehao 2005-08-31
  • 打赏
  • 举报
回复
To 楼上各位大哥:
小弟不懂.net,所以需要一个完整的实例,就是一个拷贝过来直接就能运行的,要求可能有点不合适,但是确实是需要啊,谢谢上面各位大哥的指点.
hzw66 2005-08-28
  • 打赏
  • 举报
回复
//参数:网址,发送的数据,返回的实际编码
public static string OpenURL(string url,string sendData,Encoding encode)
{
WebRequest req = WebRequest.Create(url);
if(sendData != "")
{
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = sendData.Length;
byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(sendData);
Stream stream = req.GetRequestStream();
stream.Write(buffer,0,buffer.Length);
stream.Close();
}
WebResponse resp = req.GetResponse();
return new StreamReader(resp.GetResponseStream(),encode).ReadToEnd();
}
胖河马 2005-08-28
  • 打赏
  • 举报
回复

string url = "http://abc.com/xyz.aspx";
byte[] postData = System.Text.Encoding.Default.GetBytes("username=abc&password=123");

System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "post";
request.ContentLength = postData.Length;

System.IO.Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
requestStream.Close();

System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();

System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(),System.Text.Encoding.Default);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
response.Close();
conan19771130 2005-08-27
  • 打赏
  • 举报
回复
兄弟,不要开口就要程序,帮你顶吧

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧