WebClient and HttpWebRequest (401) Unauthorized

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
YO
I get (401) Unauthorized
when i use method = "POST";
but when i use method = "PUT";
it works, but asd.php cant use that
in the code below i have tried 2 different ways


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Net;
<span style="color:Blue; using System.Security.Cryptography.X509Certificates;
<span style="color:Blue; using System.Net.Security;
<span style="color:Blue; using System.Net.Sockets;
<span style="color:Blue; using System.Reflection;
<span style="color:Blue; using System.Collections.Specialized;

<span style="color:Blue; namespace SendFilesToServer
{
<span style="color:Blue; class Program
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
<span style="color:Blue; try
{
doWebClientStuff();
}
<span style="color:Blue; catch (Exception e)
{
StreamWriter sw = <span style="color:Blue; new StreamWriter(File.OpenWrite(filePath));
sw.Write(e.ToString());
sw.Close();
}

}
<span style="color:Blue; static <span style="color:Blue; public <span style="color:Blue; void doWebClientStuff()
{
MyWebClient client = <span style="color:Blue; new MyWebClient();
<span style="color:Blue; string[] files = Directory.GetFiles(<span style="color:#A31515; "files");
SetBypassSslCertificateValidation();
client.UseDefaultCredentials = <span style="color:Blue; false;
client.Credentials = <span style="color:Blue; new NetworkCredential(<span style="color:#A31515; "name", <span style="color:#A31515; "pass");
<span style="color:Blue; string authInfo = <span style="color:#A31515; "name:pass";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
client.Headers[<span style="color:#A31515; "Authorization"] = <span style="color:#A31515; "Basic " + authInfo;
<span style="color:Blue; string url = <span style="color:#A31515; "https://ip/asd.php";


CookieContainer cookies = <span style="color:Blue; new CookieContainer();
NameValueCollection querystring = <span style="color:Blue; new NameValueCollection();
querystring[<span style="color:#A31515; "uname"] = <span style="color:#A31515; "name";
querystring[<span style="color:#A31515; "passwd"] = <span style="color:#A31515; "pass";

<span style="color:Blue; string text = <span style="color:#A31515; "";

<span style="color:Blue; byte[] bytes = <span style="color:Blue; new <span style="color:Blue; byte[0];
<span style="color:Blue; foreach (<span style="color:Blue; string uploadfile <span style="color:Blue; in files)
{
<span style="color:Green; //bytes = client.UploadFile(url, uploadfile);
text = UploadFileEx(uploadfile, url, <span style="color:#A31515; "uploadedfile", <span style="color:#A31515; "application/octet-stream", querystring, cookies);
<span style="color:Blue; break;
}
StreamWriter sw = <span style="color:Blue; new StreamWriter(File.OpenWrite(filePath2));
<span style="color:Green; //sw.Write(Encoding.ASCII.GetString(bytes));
sw.Write(text);
sw.Close();
client.Dispose();
}

<span style="color:Green; /*skip certification*/
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; void SetBypassSslCertificateValidation()
{
ServicePointManager.ServerCertificateValidationCallback
+= <span style="color:Blue; new RemoteCertificateValidationCallback(BypassSslCertificateValidation);
}

<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; bool BypassSslCertificateValidation(<span style="color:Blue; object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
<span style="color:Blue; return <span style="color:Blue; true;
}

<span style="color:Green; /*Not mine*/
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; string UploadFileEx(<span style="color:Blue; string uploadfile, <span style="color:Blue; string url,
<span style="color:Blue; string fileFormName, <span style="color:Blue; string contenttype, NameValueCollection querystring,
CookieContainer cookies)
{
<span style="color:Blue; if ((fileFormName == <span style="color:Blue; null) ||
(fileFormName.Length == 0))
{
fileFormName = <span style="color:#A31515; "file";
}

<span style="color:Blue; if ((contenttype == <span style="color:Blue; null) ||
(contenttype.Length == 0))
{
contenttype = <span style="color:#A31515; "application/octet-stream";
}


<span style="color:Blue; string postdata;
postdata = <span style="color:#A31515; "?";
<span style="color:Blue; if (querystring != <span style="color:Blue; null)
{
<span style="color:Blue; foreach (<span style="color:Blue; string key <span style="color:Blue; in querystring.Keys)
{
postdata += key + <span style="color:#A31515; "=" + querystring.Get(key) + <span style="color:#A31515; "&";
}
}
Uri uri = <span style="color:Blue; new Uri(url + postdata);


<span style="color:Blue; string boundary = <span style="color:#A31515; "----------" + DateTime.Now.Ticks.ToString(<span style="color:#A31515; "x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.CookieContainer = cookies;
webrequest.ContentType = <span style="color:#A31515; "multipart/form-data; boundary=" + boundary;
webrequest.Method = <span style="color:#A31515; "POST";


<span style="color:Green; // Build up the post message header

StringBuilder sb = <span style="color:Blue; new StringBuilder();
sb.Append(<span style="color:#A31515; "--");
sb.Append(boundary);
sb.Append(<span style="color:#A31515; "rn");
sb.Append(<span style="color:#A31515; "Content-Disposition: form-data; name="");
sb.Append(fileFormName);
sb.Append(<span style="color:#A31515; ""; filename="");
sb.Append(Path.GetFileName(uploadfile));
sb.Append(<span style="color:#A31515; """);
sb.Append(<span style="color:#A31515; "rn");
sb.Append(<span style="color:#A31515; "Content-Type: ");
sb.Append(contenttype);
sb.Append(<span style="color:#A31515; "rn");
sb.Append(<span style="color:#A31515; "rn");

<span style="color:Blue; string postHeader = sb.ToString();
<span style="color:Blue; byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);

<span style="color:Green; // Build the trailing boundary string as a byte array

<span style="color:Green; // ensuring the boundary appears on a line by itself

<span style="color:Blue; byte[] boundaryBytes =
Encoding.ASCII.GetBytes(<span style="color:#A31515; "rn--" + boundary + <span style="color:#A31515; "rn");

FileStream fileStream = <span style="color:Blue; new FileStream(uploadfile,
FileMode.Open, FileAccess.Read);
<span style="color:Blue; long length = postHeaderBytes.Length + fileStream.Length +
boundaryBytes.Length;
webrequest.ContentLength = length;

Stream requestStream = webrequest.GetRequestStream();

<span style="color:Green; // Write out our post header

requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

<span style="color:Green; // Write out the file contents

<span style="color:Blue; byte[] buffer = <span style="color:Blue; new Byte[<span style="color:Blue; checked((<span style="color:Blue; uint)Math.Min(4096,
(<span style="color:Blue; int)fileStream.Length))];
<span style="color:Blue; int bytesRead = 0;
<span style="color:Blue; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);

<span style="color:Green; // Write out the trailing boundary

requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
WebResponse responce = webrequest.GetResponse();
Stream s = responce.GetResponseStream();
StreamReader sr = <span style="color:Blue; new StreamReader(s);

<span style="color:Blue; return sr.ReadToEnd();
}

}
<span style="color:Blue; class MyWebClient : WebClient
{
<span style="color:Blue; protected <span style="color:Blue; override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)<span style="color:Blue; base.GetWebRequest(address);
request.ClientCertificates.Add(<span style="color:Blue; new X509Certificate(<span style="color:#A31515; "certificate.crt"));

<span style="color:Blue; return request;
}
<span style="color:Blue; public WebRequest asd(Uri addr)
{
<span style="color:Blue; return <span style="color:Blue; this.GetWebRequest(addr);
}
}
}

[/code]
<br/>
<br/>


View the full article
 
Back
Top