How can i setup socks proxy for ASIHTTPRequest in xcode ios? - ios

i want to UIWebView transmit all data through socks proxy i already setup proxy url,port,username and password for ASIHTTPRequest but it doesn't work and i dont want to use pac file.
$- (IBAction)Go:(id)sender {
NSURL *url = [NSURL URLWithString:[TextURL.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url];
[request1 setProxyHost:#"test.org"];
[request1 setProxyPort:808];
[request1 setUsername:#"user"];
[request1 setPassword:#"password"];
[request1 setDelegate:self];
[request1 startAsynchronous];
[TextURL resignFirstResponder];
}
$- (void)requestFinished:(ASIHTTPRequest *)request{
NSData *responseData = [request responseData];
NSError *error = [request error];
if (!error) {
[web1 loadData:responseData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[request url]];
}
}

Related

How will I convert this ASIFormDataRequest into AFNetworking?

This is the code Im using for ASIHTTPRequest I want to convert this to AFNetworking
NSURL *url = [NSURL URLWithString:path];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request addPostValue:self.theNewJob.jobID forKey:#"form[job_id]"];
[request addPostValue:thisJTF.typeFieldID forKey:#"form[jtf_id]"];
[request setFile:fullPath forKey:#"form[userfile]"];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
if ([response isEqualToString:#"SUCCESS"]) {
// debugger
NSLog(#"signature file uploaded");
} else {
// debugger
NSLog(#"signature file upload Fail: %# response: %#", error, response);
}
}
You need to replace all methods manually. I looked for it and I think below link is good to start: http://door3.com/insights/ios-programming-converting-asihttprequest-afnetworking

ASIFormDataRequest returns NULL when trying to request a POST method

Hi im trying to make a POST request
my code:
NSURL *url = [NSURL URLWithString:urlString];
__weak ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setRequestMethod:#"POST"];
[request setPostValue:#"JustinBieber" forKey:#"fname"];
[request setCompletionBlock:^{
NSString *result = [request responseString];
NSDictionary *dict = [result JSON];
NSLog(#"dict -%#",dict);
}];
[request setFailedBlock:^{
NSLog(#"error %#",[request error]);
}];
[request startAsynchronous];
when I run my code it returns a (null) value. My urlString is correct and the request didn't give me error also. I've tried it on web and returns a {"status":"success"} (it will return a dictionary with status successful or failed).
Use Like this. Hope this will help.
-(void)exe method
{
NSString *strURL=#"---your URL----";
NSURL *url=[NSURL URLWithString:strURL];
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:#"JustinBieber" forKey:#"fname"];
[request setDelegate:self];
[request setTimeOutSeconds:60];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSError *error;
if(!error)
{
NSString *receivedString = [request responseString];
NSDictionary *dic = [receivedString JSONValue];
NSLog(#"output %#",dic);
}
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
}
Try with this code -
NSURL *url = [NSURL URLWithString:urlString];
__unsafe_unretained ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:#"JustinBieber" forKey:#"fname"];
[request setDelegate:self];
__block id jsonData;
[request setTimeOutSeconds:300];
[request setCompletionBlock:^(){
NSError *error = nil;
NSString *responseString = (request.responseString.length)?request.responseString:#"";
NSLog(#"%#",responseString);
NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
jsonData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
if(error)
completionBlock(nil, error, task);
else
completionBlock(jsonData, error, task);
}];
[request setFailedBlock:^{
completionBlock(nil, request.error, task);
}];
[request startAsynchronous];
Might be it will helpfull for you.
Did you set your headers in the script that returns JSON correctly? Assuming you're using PHP:
header('Content-Type: application/json');
The default MIME-type is "text/plain", instead of "application/json". If you dont set the MIME-type correctly you're basicly trying to parse the whole document.
So instead of:
{"status":"success"}
You are most likely trying to parse:
<html>
<head></head>
<body>{"status":"success"}</body>
</html>
-(void)exe method
{
NSString *strURL=#"---your URL----";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
[request setDelegate:self];
[request setRequestMethod:#"POST"];
[request setPostValue:#"JustinBieber" forKey:#"fname"];
[request setTimeOutSeconds:60];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSString *receivedString = [request responseString];
NSLog(#"output %#",receivedString );
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSString *receivedString = [request responseString];
NSLog(#"output %#",receivedString );
}

How to submit text messages to server via ios apps

my problem is I cannot send the text messages to server. Im using asihttprequest. I have define the USERNAME, UUID, PASSWORD, API_PASSWORD and NUMBER myself. What I want to do is just send the data to the server url given. Here is my code:
- (IBAction)sendClicked:(id)sender {
[sendButton resignFirstResponder];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"https://cc.frifon.net_dosmssend/"]];
[request setDelegate:self];
[request setNumberOfTimesToRetryOnTimeout:3];
[request setRequestMethod:#"POST"];
[request setPostValue:USERNAME forKey:#"sip"];
[request setPostValue:PASSWORD forKey:#"pwd"];
[request setPostValue:UUID forKey:#"uuid"];
[request setPostValue:API_PASSWORD forKey:#"key"];
[request setPostValue:messageText forKey:#"message"];
[request setPostValue:NUMBER forKey:#"to"];
[request setPostValue:#"Submit" forKey:#"submit"];
[request start];
nil;
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
messageText.text = response;
}
}
Try this,
- (IBAction)sendClicked:(id)sender {
[sendButton resignFirstResponder];
NSString *serviceString = https://cc.frifon.net_dosmssend/;
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[serviceString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
[request setDelegate:self];
request.requestMethod = #"POST";
request.timeOutSeconds = 30.0;
[request addPostValue:USERNAME forKey:#"sip"];
[request addPostValue:PASSWORD forKey:#"pwd"];
[request addPostValue:UUID forKey:#"uuid"];
[request addPostValue:API_PASSWORD forKey:#"key"];
[request addPostValue:messageText forKey:#"message"];
[request addPostValue:NUMBER forKey:#"to"];
[request addPostValue:#"Submit" forKey:#"submit"];
[request startAsynchronous];
}
- (IBAction)sendClicked:(id)sender {
// Add you all data in below dictionary
NSDictionary *dictionary = #{#"action":#"login",#"nick_name":nikname,#"password":pass};
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString *jason =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"JSON summary: %#", jason);
NSString *address = [NSString stringWithFormat:#"%#",#"www.demo.com"];
address= [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:address];
NSLog(#"%#",address);
// request creation
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setHTTPBody:data];
responseData = [[NSMutableData alloc] init];
NSURLConnection *Conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[Conn start];
}

NSMutableURLRequest transform to ASIFormDataRequest

I have writen the fellowing code:
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
postStr = #"user_name=Thomas Tan&phone=01234567891&password=123456";
NSData *myRequestData = [NSData dataWithBytes:[postStr UTF8String] length:[postStr length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: myRequestData];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(#"%#",responseString);
it works well,but now I want to use asihttprequest framework,so how to change the above code,I have writen the code,but it can't get the correct result and just get the server error infomation.so what's the problem?
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *requeset = [ASIFormDataRequest requestWithURL:url];
[requeset setRequestMethod:#"POST"];
[requeset setPostValue:#"Thomas Tan" forKey:#"user_name"];
[requeset setPostValue:#"01234567891" forKey:#"phone"];
[requeset setPostValue:#"123456" forKey:#"password"];
[requeset startSynchronous];
NSError *error = [requeset error];
if (!error) {
NSString *re = [requeset responseString];
NSLog(#"%#",re);
}
NSLog(#"%#",error);
thank you in advance.
UPDATE:
NSString *urlString = [NSString stringWithFormat:ADDRESS,action];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request appendPostData:[#"user_name=Thomas Tan&phone=01234567891&password=123456" dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *re = [request responseString];
NSLog(#"%#",re);
}
NSLog(#"%#",error);
I use the above code ,It also can't get the same result,and error is not nil.
Your ASIHTTP code is not doing the same thing as your NSURLConnection code.
ASIFormDataRequest will automatically:
set the Content-Type header to application/x-www-form-urlencoded
URL-encoded your parameters
That's usually exactly what you want, but if you're getting the correct behavior with your NSURLConnection code and incorrect with ASIHTTP, then you need to change to a custom ASIHTTP POST and use ASIHTTPRequest, not ASIHTTPFormDataRequest, and then manually set the Conten-type back to application/x-www-form-urlencoded:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Content-Type" value:#"application/x-www-form-urlencoded"];
[request appendPostData:[#"user_name=Thomas Tan&phone=01234567891&password=123456" dataUsingEncoding:NSUTF8StringEncoding]];
Doing this, and inspecting exactly what was sent to the server using Wireshark, I can see that the POST data sent is still not quite identical (ASIHTTP on the left, NSURLConnection on the right):
But the content type, length, and actual data is identical.
At this point, I'd expect your server to return the same result.
If it still doesn't, you can edit the ASIhTTP request parameters to match.

ASIHTTPRequest POST to TomCat

When i use ASIHTTPRequest to "POST" to server URI, just response back "null".
URI: "http://www.solok.com:8080/solok_interface/api/web/"
parameter: NSDictionary with key "content".
I try to use ASIHTTPRequest instead of ASIFormDataRequest, but there's no "setPostValue" method.
Any help, thanks!
NSURL * url = [NSURL URLWithString:#"http://localhost:8080/solok_interface/api/web/"];
ASIFormDataRequest *req = [ASIFormDataRequest requestWithURL:url];
[req setRequestMethod:#"POST"];
[req setPostValue:cipherString forKey:#"content"];
[req start];
NSError *error1 = [req error];
if (!error1) {
NSString *reponse = [req responseString];
NSLog(#"Response string is %#",reponse);
}
[req setDelegate:self];
[req setCompletionBlock:^{
NSLog(#"complete");
NSString *responseString = [req responseString];
NSLog(#"the string is %#",responseString);
NSLog(#"The data is %# %d",[req responseStatusMessage],[req responseStatusCode]);
}];
[req setFailedBlock:^{
NSLog(#"#fail");
}];
I didn't use ASIHTTPRequest anymore, the ASI team has stopped to support it. AFNetworking is the right choice for IOS developer.

Resources