HttpGet和HttpPost请求

internal static string HttpPost(string Url, string postDataStr)
{
string retString = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);

if ((!string.IsNullOrEmpty(webProxy)))
{
WebProxy proxyObject = new WebProxy(webProxy, Int32.Parse(webProxyPort));
proxyObject.Credentials = new NetworkCredential(username, password, domain);
request.Proxy = proxyObject;
}
log.InfoFormat("username:{0};password:{1};domain:{2};webProxyPort:{3};Url:{4};PostData:{5}", username,
password, domain, webProxyPort, Url, postDataStr);
//log.InfoFormat("username:{0};password:{1};domain:{2};webProxyPort:{3}", username, password, domain, webProxyPort);
//request.CookieContainer = cookie;

byte[] bytes = Encoding.UTF8.GetBytes(postDataStr);

Stream myRequestStream = request.GetRequestStream();
myRequestStream.Write(bytes, 0, bytes.Length);
myRequestStream.Close();

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

//response.Cookies = cookie.GetCookies(response.ResponseUri);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
retString = myStreamReader.ReadToEnd();

myStreamReader.Close();
myResponseStream.Close();
//log.Info("doPostSuccess" + retString);

}
catch (Exception ex)
{
log.ErrorFormat("HttpPost_Error!Url:{0};postDataStr:{1},ErrorMessage:{2}", Url, postDataStr, ex.Message);
}

return retString;
}

/// <summary>
/// 通过GET方式发送数据
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
public static string HttpGet(string Url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";

if ((!string.IsNullOrEmpty(webProxy)))
{
WebProxy proxyObject = new WebProxy(webProxy, Int32.Parse(webProxyPort));
proxyObject.Credentials = new NetworkCredential(username, password, domain);
request.Proxy = proxyObject;
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}

时间: 2024-10-10 16:17:05

HttpGet和HttpPost请求的相关文章

java最简单的方式实现httpget和httppost请求

java实现httpget和httppost请求的方式多种多样,个人总结了一种最简单的方式,仅仅需几行代码,就能够完美的实现. 此处须要用到两个jar包,httpclient-4.3.1.jar.httpcore-4.3.jar,各位能够到网上自己下载,或者到我的附件里下载,以下先贴上httpget请求的代码: String url = ""; HttpGet request = new HttpGet(url); try { HttpResponse response = HttpC

转-浅谈HTTP-GET 、 HTTP-POST 和SOAP

原文链接:浅谈HTTP-GET . HTTP-POST 和SOAP 1.HTTP-GET 和 HTTP-POST HTTP-GET和HTTP-POST是标准协议,他们使用HTTP(超文本传输协议)谓词(谓词是指条件表达式的求值返回真或假的过程.)对参数金星编码并将参数作为名称/值对传递,还使用关联的请求语义.每个协议都包含一系列HTTP请求标头,HTTP请求标头及其他一些信息定义客户端向服务器请求哪些内容,哪个服务器用一系列HTTP响应标头和所请求的数据进行响应. HTTP-GET 使用 MIM

linux下HttpGet、HttpPost的C++实现

#include "HttpRequest.h" int main() { HttpRequest* Http = new HttpRequest; char* str = (char*)malloc(BUFSIZE); memset(str, 0, BUFSIZE); if(Http->HttpGet("http://www.baidu.com", str)) { printf("%s\n", str); } else { printf(

HTTPClient模块的HttpGet和HttpPost

无论是使用HttpGet,还是使用HttpPost,都必须通过如下3步来访问HTTP资源. 1.创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象. 2.使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST请求,并返回HttpResponse对象. 3.通过HttpResponse接口的getEntity方法返回响应信息,并进行相应的处理. 如果使用HttpPost方法提交HTTP POS

基于人工分析的HTTP-POST请求报文特征获取一般方法

基于人工分析的HTTP-POST请求报文特征获取一般方法 (以百度贴吧客户端发帖行为分析为例) 本文由CSDN-蚍蜉撼青松 [主页:http://blog.csdn.net/howeverpf]原创,转载请注明出处! 现有的HTTP-POST请求报文信息还原工具都是基于特征(包括网络应用的行为识别特征与信息提取特征)的,而特征的获取往往依赖于人工分析.在分析的过程中往往需要用到一些能帮助完成网络数据采集和分析功能的工具.根据网络环境的不同,可能用到的几款工具包括: Tcpdump(Unix/Li

httpPost请求用java实现的方法

package com.day3.sample; //首先下面我我们需要导入的jar包和文件 import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.Consts; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException;

Android中使用HttpGet和HttpPost访问HTTP资源

需求:用户登录(name:用户名,pwd:密码) (一)HttpGet :doGet()方法//doGet():将参数的键值对附加在url后面来传递 [java] view plaincopy public String getResultForHttpGet(String name,String pwd) throws ClientProtocolException, IOException{ //服务器  :服务器项目  :servlet名称 String path="http://192.

C# 使用 HttpPost 请求调用 WebService

原文:C# 使用 HttpPost 请求调用 WebService 之前调用 WebService 都是直接添加服务引用,然后调用 WebService 方法的,最近发现还可以使用 Http 请求调用 WebService.这里还想说一句,还是 web api 的调用简单. WebService 服务端代码: public class WebServiceDemo : System.Web.Services.WebService { [WebMethod] public string Hello

Android:HttpGet与HttpPost

AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET"/> activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&