后台代码怎么写实现get方式请求服务器某一页面,并得到返回值

godhuang 2010-03-16 12:40:23
后台代码怎么写实现get方式请求服务器某一页面,并得到返回值
...全文
563 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
大笨蛋 2010-03-16
  • 打赏
  • 举报
回复
接口...不是webservice么...- -!
tkscascor 2010-03-16
  • 打赏
  • 举报
回复
额. 你调用接口 ? 调用页面?
godhuang 2010-03-16
  • 打赏
  • 举报
回复
如果两个页面是我自己同一个程序那简单了,可以用楼上的做,问题是两个页面根本没关系的,就是接口问题啊
tkscascor 2010-03-16
  • 打赏
  • 举报
回复
a a页面做b页面的事情不就行了吗?
b function d(returnvalue)
godhuang 2010-03-16
  • 打赏
  • 举报
回复
正因为不知道怎么写代码所以问大家,如果有更好的解决方案那我跟欢迎
tkscascor 2010-03-16
  • 打赏
  • 举报
回复
我怎么觉得你这是脱了裤子放屁,,, 都后台代码了,直接运行 那个页面要干的事情..
godhuang 2010-03-16
  • 打赏
  • 举报
回复
如果不用ajax的话要怎么处理
godhuang 2010-03-16
  • 打赏
  • 举报
回复
请求的服务器页面是个普通的页面,不是webservice
inmyownsky1 2010-03-16
  • 打赏
  • 举报
回复
楼主用下ajax
大笨蛋 2010-03-16
  • 打赏
  • 举报
回复
用webservice吧.
阿非 2010-03-16
  • 打赏
  • 举报
回复
有什么问题
godhuang 2010-03-16
  • 打赏
  • 举报
回复
在这里也说不清楚,给你们发消息了,看到留个言,谢谢
godhuang 2010-03-16
  • 打赏
  • 举报
回复
上面1星和4星高手的有没有联系方式
tkscascor 2010-03-16
  • 打赏
  • 举报
回复
嗯. 如果是调用页面 就是上面的代码了, 你参照一下就行了.
其实如果是要引入别人的页面, 如果不做处理的话,, 镶嵌一个iframe可以偷懒...
阿非 2010-03-16
  • 打赏
  • 举报
回复
恩,就是用 HttpWebRequest

zhujiazhi 2010-03-16
  • 打赏
  • 举报
回复
public string PostLogin(string strURL, string strArgs, string strReferer)
{
string strResult = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
myHttpWebRequest.AllowAutoRedirect = true;
myHttpWebRequest.KeepAlive = true;
myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
myHttpWebRequest.Referer = strReferer;

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

CookieCollection myCookies = null;
CookieContainer myCookieContainer = new CookieContainer();
myHttpWebRequest.CookieContainer = myCookieContainer;

Stream MyRequestStrearm = myHttpWebRequest.GetRequestStream();
StreamWriter MyStreamWriter = new StreamWriter(MyRequestStrearm, Encoding.ASCII);
//把数据写入HttpWebRequest的Request流
MyStreamWriter.Write(strArgs);
//关闭打开对象
MyStreamWriter.Close();
MyRequestStrearm.Close();

HttpWebResponse response = null;
StreamReader sr = null;
response = (HttpWebResponse)myHttpWebRequest.GetResponse();

string cookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(new Uri(strURL));
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["cookieHeader"] = cookieHeader;
HttpContext.Current.Application.UnLock();
myCookies = response.Cookies;

sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")); // //utf-8
strResult = sr.ReadToEnd();
return strResult;
}

public void getPage(String url, String paramList, TextBox paramTextBox)
{
WebResponse result = null;

try
{

WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = { '?', '=', '&' };
byte[] SomeBytes = null;

if (paramList != null)
{
int i = 0, j;
while (i < paramList.Length)
{
j = paramList.IndexOfAny(reserved, i);
if (j == -1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
UrlEncoded.Append(paramList.Substring(j, 1));
i = j + 1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}


result = req.GetResponse();
Stream ReceiveStream = result.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);
paramTextBox.Text += str;
count = sr.Read(read, 0, 256);
}
ReceiveStream.Close();
}
catch (Exception e)
{
paramTextBox.Text = e.ToString();
}
finally
{
if (result != null)
{
result.Close();
}
}
}
godhuang 2010-03-16
  • 打赏
  • 举报
回复
private string requestget(string theurl)
{
Uri lcUri = new Uri(theurl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(lcUri);
string page;
try
{
//request.KeepAlive = false;
//request.ProtocolVersion = HttpVersion.Version10;
request.Method = "GET";
//request.ContentType = "application/x-www-form-urlencoded";
//request.Proxy = WebProxy.GetDefaultProxy();
//request.AllowAutoRedirect = true;
//request.MaximumAutomaticRedirections = 10;
//request.Timeout = (int)new TimeSpan(0, 0, 60).TotalMilliseconds;
//request.UserAgent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)";

HttpWebResponse res = (HttpWebResponse)request.GetResponse();

Stream st = res.GetResponseStream();
StreamReader sr = new StreamReader(st, Encoding.Default);
page = sr.ReadToEnd();
sr.Close();
res.Close();
}
catch(Exception ee)
{
page = "fail message : " + ee.Message;
}
return page;
}

我网上找了这么个方法,高手们看看对不?
zhujiazhi 2010-03-16
  • 打赏
  • 举报
回复
webrequest
godhuang 2010-03-16
  • 打赏
  • 举报
回复
tkascor 给我发个QQ号,帮忙解决下
jack15850798154 2010-03-16
  • 打赏
  • 举报
回复
支持13楼。。。。。。。。。。。。。。。。
加载更多回复(5)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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