Get HttpRequest like Fiddler - asp.net

What's the easiest way to convert the entire HttpWebRequest to a single string. Similarly to what Fiddler displays when you switch the Fiddler's request pane to the Raw tab?
Thanks for your advice.

Use System.Net.WebClient.
This code combines the HTTP Headers and Body for a Url similar to Fiddler:
string url = "http://stackoverflow.com";
var webClient = new System.Net.WebClient();
var body = webClient.DownloadString(url);
var headers = webClient.ResponseHeaders.ToString();
var raw = headers + "\r\n" + body;

Related

WP Rest API - Upload image to WP using Restsharp

I have a problem uploading an image to Wordpress from a VB .NET project (using Restsharp). I create the client and the request for this, I added a header with the authorization, parameters...) but, when I execute the request, this response Status OK (200) but the image has not create in Wordpress.
I tried all this sentences, and no works:
Test 1:
Dim client As RestClient = New RestClient("http://domain-example.com/wp-json/wp/v2/media")
client.Timeout = -1
Dim request As RestRequest = New RestRequest(Method.POST)
request.AddHeader("Authorization", "Basic {base64code}")
request.AddHeader("Cookie", "PHPSESSID=b83jbtsfjbb2bkkso7s75m75il")
request.AddHeader("Content-Disposition", "attachment; filename=Google-logo.jpg")
request.AddHeader("Content-Type", "image/jpeg")
request.AddFile("file", "C:\temp\Google-logo.jpg")
request.AddParameter("title", "titleExample")
request.AddParameter("caption", "captionExample")
Dim response As IRestResponse = client.Execute(request)
Console.WriteLine(response.StatusCode)
Test 2:
Dim client As RestClient = New RestClient("http://domain-example.com/wp-json/wp/v2/media")
client.Timeout = -1
Dim request As RestRequest = New RestRequest(Method.POST)
request.AddHeader("Authorization", "Basic {base64code}")
request.AddHeader("Cookie", "PHPSESSID=b83jbtsfjbb2bkkso7s75m75il")
request.AddParameter("title", "titleExample")
request.AddParameter("caption", "captionExample")
request.AlwaysMultipartFormData = True
request.AddParameter("file", "C:\temp\Google-logo.png")
Dim response As IRestResponse = client.Execute(request)
Console.WriteLine(response.StatusCode)
Test 3:
Dim client as RestClient = New RestClient("http://domain-example.com/wp-json/wp/v2/media")
client.Timeout = -1
Dim request = New RestRequest(Method.POST)
request.RequestFormat = DataFormat.Json
request.AddHeader("Authorization", "Basic {base64code}")
request.AddFileBytes("file", BytesImage, "C:\temp\Google-logo.jpg", "image/jpeg")
request.AddParameter("title", "tempFile")
request.AddParameter("caption", "tempFileCaption")
Dim response As IRestResponse = client.Execute(request)
Console.WriteLine(response.Content)
Test 4: In this example I not use RestSharp, I used the HttpWebRequest, and the same result
Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse
myReq = HttpWebRequest.Create("http://domain-example.com/wp-json/wp/v2/media")
myReq.Method = "POST"
myReq.ContentType = "application/json"
myReq.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("user:password")))
Dim myData As String = "c:\temp\Google-logo.jpg"
myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
myResp = myReq.GetResponse
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
Dim myText As String
myText = myreader.ReadToEnd
I tried to simulate the upload using Postman, but I can't.
I don't know why it's so hard to upload an image to Wordpress using REST...
Disclaimer:
Also, this post doesn't work for me
The following is from the docs.
To add a file to the request you can use the RestRequest function called AddFile. The main function accepts the FileParameter argument:
request.AddFile(fileParameter);
You can instantiate the file parameter using FileParameter.Create that accepts a bytes array or FileParameter.FromFile, which will load the file from disk.
There are also extension functions that wrap the creation of FileParameter inside:
// Adds a file from disk
AddFile(parameterName, filePath, contentType);
// Adds an array of bytes
AddFile(parameterName, bytes, fileName, contentType);
// Adds a stream returned by the getFile function
AddFile(parameterName, getFile, fileName, contentType);
Remember that AddFile will set all the necessary headers, so please don't try to set content headers manually. Your code sets a lot of content headers, and it's unnecessary, and might be breaking your requests.
You can always use https://requestbin.com and send your requests there to inspect the content of those requests, so you can see if they match the expected request format.
In test 1, remove or comment out this line of code:
request.AddHeader("Content-Type", "image/jpeg")
The solution for this is activate the JWT authentication plugin for Wordpress. By default, Wordpress avoid any POST call, the basic authentication doesn't work.
So, once activated the JWT (following the process), you must create a Token (using POST to the JWT endpoint) and put the Token in the POST process to create anything (posts, media, etc.)

mvaayoo api issue for sending messages from a website

i am using mvaayoo api for sending the messages from my website.
i have read the documentation and do the same even then i am not able to send the messages.
i am using this sample code
string strUrl = "http://api.mVaayoo.com/mvaayooapi/MessageCompose?user=
Username:Password&senderID=mVaayoo&receipientno=919849558211&msgtxt=This is a test from mVaayoo API&state=4";
WebRequest request = HttpWebRequest.Create(strUrl);
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse();
Stream s = (Stream)response.GetResponseStream();
StreamReader readStream = new StreamReader( s );
string dataString = readStream.ReadToEnd();
response.Close();
s.Close();
readStream.Close();
Help me please
Thanks,
Rajbir
I think the problem is because of the space in URL.
Use this for URL ;
string strUrl = "http://api.mVaayoo.com/mvaayooapi/MessageCompose?user=
Username:Password&senderID=mVaayoo&receipientno=919849558211&msgtxt=This%20is%20a%20test%20from%20mVaayoo%20API&state=4";
I replaced all space with %20 in the URL. I suffered with the same problem in java.. so it should work..

Post Comment on wordpress Blogs

I was working on a project in which I have to post comment on by wordpress blog that should contain the text user entered in text box.I have been trying to user HttpWebRequestbut it fails and returns 404 not found. Even the link is not broken.here is my code
fore test purpose i have hard coded the entries in string post
string post = "author=" + HttpUtility.UrlEncode("afnan") + "&email=" + HttpUtility.UrlEncode("ifi#ifi.com") + "&url=" + HttpUtility.UrlEncode("abcd.com") +
"&comment=" + HttpUtility.UrlEncode("no comments");
HttpWebRequest wrWebRequest = WebRequest.Create("http://testing.autoprofitbot.com/blogtest/2011/05/13/call-3-computer-repair-services-put-to-test-4/wp-comments-post.php?") as HttpWebRequest;
wrWebRequest.Method = "POST";
wrWebRequest.ContentLength = post.Length;
wrWebRequest.ContentType = "application/x-www-form-urlencoded";
wrWebRequest.CookieContainer = new CookieContainer();
//// Post to the login form.
StreamWriter swRequestWriter = new
StreamWriter(wrWebRequest.GetRequestStream());
swRequestWriter.Write(post);
swRequestWriter.Close();
// Get the response.
HttpWebResponse hwrWebResponse =
(HttpWebResponse)wrWebRequest.GetResponse();
// Have some cookies.
CookieCollection ccCookies = hwrWebResponse.Cookies;
// Read the response
StreamReader srResponseReader = new
StreamReader(hwrWebResponse.GetResponseStream());
string strResponseData = srResponseReader.ReadToEnd();
srResponseReader.Close();
webBrowser1.DocumentText = strResponseData;
There are 3 problems with your code:
the post data lacks 2 parameters
the WebRequest.Create is not correct
the referrer url is missing; use livehttpheaders or httpfox to get the correct headers format!

What is wrong with my auto forum thread generator using HttpWebRequest?

I'm using HttpWebRequest to send request without using a browser but I always get this reply from this old school cgi forum: "duplicate thread". I'm pretty sure the thread is not a duplicate as it is the first time I send it. I'm guessing the forum software is detecting something unusual about my thread as is is machine generated. What can I do?
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://pop.6park.com/cgi-bin/know1/mainboard.pl");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
string postData = "name=ZhuangNan&usrpwd=aaa&subject=whatisthis&body=abcabcabcabc";
UTF8Encoding encoding = new UTF8Encoding();
byte[] byte1 = encoding.GetBytes(postData);
myRequest.ContentLength = byte1.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(byte1, 0, byte1.Length);
If the target script tries to check the UserAgent HTTP header to determine if the request is being made by a browser, you can try populating the HttpWebRequest.UserAgent property with a valid value. This has worked for me on at least one occasion.
There are a number of other HTTP headers that you could set to make the target script think it is being hit by a browser rather than a program.
Check out any of the HTTP header properties that can be set. See HttpWebRequest Properties for more information.
myRequest.ContentType = "application/x-www-form-urlencoded";
string postData = "name=ZhuangNan&usrpwd=aaa&subject=whatisthis&body=abcabcabcabc";
Not a MIME/baseenc data?

How to get status code of a POST with Asp.Net HttpWebRequest

I'm trying to ping Google when my web site's sitemap is updated but I need to know which status code does Google or any other service returns. My code is below:
HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create("http://search.yahooapis.com/ping?sitemap=http%3a%2f%2fhasangursoy.com.tr%2fsitemap.xml");
rqst.Method = "POST";
rqst.ContentType = "text/xml";
rqst.ContentLength = 0;
rqst.Timeout = 3000;
rqst.GetResponse();
You need to use the response - assign it to a HttpWebResponse variable:
HttpWebResponse resp = (HttpWebResponse)rqst.GetResponse();
HttpStatusCode respStatusCode = resp.StatusCode;
The HttpStatusCode enumeration will tell you what status code was returned.
Try HttpWebResponse.StatusCode out

Resources