本文整理汇总了C#中HttpWebRequest类的典型用法代码示例。如果您正苦于以下问题:C# HttpWebRequest类的具体用法?C# HttpWebRequest怎么用?C# HttpWebRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpWebRequest类属于命名空间,在下文中一共展示了HttpWebRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddRequestXml
private void AddRequestXml(string url, HttpWebRequest req)
{
Stream stream = (Stream)req.GetRequestStream();
using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
{
writer.WriteStartDocument(true);
writer.WriteStartElement("methodCall");
writer.WriteElementString("methodName", "pingback.ping");
writer.WriteStartElement("params");
writer.WriteStartElement("param");
writer.WriteStartElement("value");
writer.WriteElementString("string", Blogsa.Url);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("param");
writer.WriteStartElement("value");
writer.WriteElementString("string", url);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
}
示例2: set_option
private void set_option(string rel)
{
string url = "http://" + URI + "/web/cgi-bin/hi3510/param.cgi?"+rel+"&usr=" + Login + "&pwd=" + Password;
Console.WriteLine("Call "+url+" - start");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create (url);
req.KeepAlive = false;
req.Headers.Add ("Accept-Encoding", "gzip,deflate");
req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
req.UseDefaultCredentials = true;
req.Credentials = new NetworkCredential (Login, Password);
req.Proxy = null;
req.Timeout = 1000;
WebResponse resp = null;
req.KeepAlive = false;
pause = true;
try{
resp = req.GetResponse();
resp.Close();
} catch(Exception e) {
Console.WriteLine (e.Message);
}
req = null;
Console.WriteLine("Call "+url+" - end");
}
示例3: Request
public void Request(string url)
{
Stopwatch timer = new Stopwatch();
StringBuilder respBody = new StringBuilder();
this.request = (HttpWebRequest)WebRequest.Create(url);
try {
timer.Start();
this.response = (HttpWebResponse)this.request.GetResponse();
byte[] buf = new byte[8192];
Stream respStream = this.response.GetResponseStream();
int count = 0;
do {
count = respStream.Read(buf, 0, buf.Length);
if (count != 0)
respBody.Append(Encoding.ASCII.GetString(buf, 0, count));
}
while (count > 0);
timer.Stop();
this.responseBody = respBody.ToString();
this.statusCode = (int)(HttpStatusCode)this.response.StatusCode;
this.responseTime = timer.ElapsedMilliseconds / 1000.0;
} catch (WebException ex) {
this.response = (HttpWebResponse)ex.Response;
this.responseBody = "No Server Response";
this.escapedBody = "No Server Response";
this.responseTime = 0.0;
}
}
示例4: State
public State (int id, HttpWebRequest req)
{
this.id = id;
request = req;
handle = new ManualResetEvent (false);
handleList.Add (handle);
}
示例5: OnSendingHeaders
// fields
// constructors
// properties
// methods
internal static void OnSendingHeaders(HttpWebRequest httpWebRequest) {
GlobalLog.Print("CookieModule::OnSendingHeaders()");
try {
if (httpWebRequest.CookieContainer == null) {
return;
}
//
// remove all current cookies. This could be a redirect
//
httpWebRequest.Headers.RemoveInternal(HttpKnownHeaderNames.Cookie);
//
// add in the new headers from the cookie container for this request
//
string optCookie2;
string cookieString = httpWebRequest.CookieContainer.GetCookieHeader(
httpWebRequest.GetRemoteResourceUri(), out optCookie2);
if (cookieString.Length > 0) {
GlobalLog.Print("CookieModule::OnSendingHeaders() setting Cookie header to:[" + cookieString + "]");
httpWebRequest.Headers[HttpKnownHeaderNames.Cookie] = cookieString;
//<
}
}
catch {
}
}
示例6: OnReceivedHeaders
internal static void OnReceivedHeaders(HttpWebRequest httpWebRequest) {
GlobalLog.Print("CookieModule.OnReceivedHeaders()");
//
// if the app doesn't want us to handle cookies then there's nothing
// to do. Note that we're leaving open the possibility that these
// settings could be changed between the request being made and the
// response received
//
try {
if (httpWebRequest.CookieContainer == null) {
return;
}
//
// add any received cookies for this response to the container
//
HttpWebResponse response = (HttpWebResponse)httpWebRequest._HttpResponse;
CookieCollection cookies = null;
try {
string cookieString = response.Headers[HttpKnownHeaderNames.SetCookie];
GlobalLog.Print("CookieModule::OnSendingHeaders() received Set-Cookie:[" + cookieString + "]");
if ((cookieString != null) && (cookieString.Length > 0)) {
cookies = httpWebRequest.CookieContainer.CookieCutter(
response.ResponseUri,
HttpKnownHeaderNames.SetCookie,
cookieString,
false);
}
}
catch {
}
try {
string cookieString = response.Headers[HttpKnownHeaderNames.SetCookie2];
GlobalLog.Print("CookieModule::OnSendingHeaders() received Set-Cookie2:[" + cookieString + "]");
if ((cookieString != null) && (cookieString.Length > 0)) {
CookieCollection cookies2 = httpWebRequest.CookieContainer.CookieCutter(
response.ResponseUri,
HttpKnownHeaderNames.SetCookie2,
cookieString,
false);
if (cookies != null && cookies.Count != 0) {
cookies.Add(cookies2);
}
else {
cookies = cookies2;
}
}
}
catch {
}
if (cookies != null) {
response.Cookies = cookies;
}
}
catch {
}
}
示例7: InitializeRequest
public static void InitializeRequest(HttpWebRequest request)
{
request.Headers.Add("aw-tenant-code", API_TENANT_CODE);
request.Credentials = new NetworkCredential(USER_NAME, PASSWORD);
request.KeepAlive = false;
request.AddRange(1024);
request.Timeout = 10000;
}
示例8: AssertRequestDefaults
/// <summary>
/// For every request, there are things we expect to remain constant. Verify that they do.
/// </summary>
/// <param name="request"></param>
/// <param name="method"></param>
public void AssertRequestDefaults(HttpWebRequest request, string method)
{
Assert.IsTrue(request.Address == new Uri(host));
Assert.IsTrue(request.Headers.ToString() == "Authorization: Bearer\r\nContent-Type: application/json\r\n\r\n");
Assert.IsTrue(request.Host == "localhost");
Assert.IsTrue(request.KeepAlive == true);
Assert.IsTrue(request.Method == method);
Assert.IsTrue(request.Timeout == 90000);
}
示例9: getResponse
protected HttpWebResponse getResponse(HttpWebRequest request)
{
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
return response;
}
示例10: InitGCMClient
private void InitGCMClient()
{
//對GCM Server發出Http post
gcmRequest = WebRequest.Create(GCM_URI) as HttpWebRequest;
gcmRequest.ContentType = "application/json";
gcmRequest.UserAgent = "Android GCM Message Sender Client 1.0";
gcmRequest.Method = "POST";
// Credential info
gcmRequest.Headers.Add("Authorization", "key=" + APIKey);
}
示例11: GetReadStreamResult
/// <summary>
/// Constructs a new async result object
/// </summary>
/// <param name="source">The source of the operation.</param>
/// <param name="method">Name of the method which is invoked asynchronously.</param>
/// <param name="request">The <see cref="HttpWebRequest"/> object which is wrapped by this async result.</param>
/// <param name="callback">User specified callback for the async operation.</param>
/// <param name="state">User state for the async callback.</param>
internal GetReadStreamResult(
object source,
string method,
HttpWebRequest request,
AsyncCallback callback,
object state)
: base(source, method, callback, state)
{
Debug.Assert(request != null, "Null request can't be wrapped to a result.");
this.request = request;
this.Abortable = request;
}
示例12: createDataStream
public Stream createDataStream(Dictionary<string,string> body ,HttpWebRequest request)
{
string theBody = "";
foreach(KeyValuePair<string,string> bodyPart in body){
theBody = theBody + bodyPart.Key + "=" + bodyPart.Value + "&";
}
byte[] byteArray = Encoding.UTF8.GetBytes(theBody);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0,byteArray.Length);
dataStream.Close ();
return dataStream;
}
示例13: AddProxyInfoToRequest
public static HttpWebRequest AddProxyInfoToRequest(HttpWebRequest httpRequest, Uri proxyUri, string proxyId, string proxyPassword, string proxyDomain)
{
if (httpRequest != null)
{
WebProxy proxyInfo = new WebProxy();
proxyInfo.Address = proxyUri;
proxyInfo.BypassProxyOnLocal = true;
proxyInfo.Credentials = new NetworkCredential(proxyId, proxyPassword, proxyDomain);
httpRequest.Proxy = proxyInfo;
}
return httpRequest;
}
示例14: HttpWebRequestCore
public HttpWebRequestCore (HttpWebRequest parent, Uri uri)
{
if (uri == null)
throw new ArgumentNullException ("uri");
this.uri = uri;
if (parent == null) {
// special case used for policy
allow_read_buffering = true;
method = "GET";
} else {
allow_read_buffering = parent.AllowReadStreamBuffering;
method = parent.Method;
headers = parent.Headers;
}
}
示例15: GetResponse
private string GetResponse(string url, ref HttpWebRequest request)
{
StringBuilder sb = new StringBuilder();
Stream resStream = null;
HttpWebResponse response = null;
byte[] buf = new byte[8192];
try
{
// execute the request
response = (HttpWebResponse)request.GetResponse();
// we will read data via the response stream
resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
}
catch (Exception err)
{
String exc = err.Message;
}
finally
{
response.Close();
resStream.Close();
}
return sb.ToString();
}